| 
 | 1 | +.. index::  | 
 | 2 | + single: Console Helpers; Cursor Helper  | 
 | 3 | + | 
 | 4 | +Cursor Helper  | 
 | 5 | +=============  | 
 | 6 | + | 
 | 7 | +.. versionadded:: 5.1  | 
 | 8 | + | 
 | 9 | + The :class:`Symfony\\Component\\Console\\Cursor` class was introduced  | 
 | 10 | + in Symfony 5.1.  | 
 | 11 | + | 
 | 12 | +The :class:`Symfony\\Component\\Console\\Cursor` allows you to change the  | 
 | 13 | +cursor position in a console command. This allows you to write on any position  | 
 | 14 | +of the output:  | 
 | 15 | + | 
 | 16 | +.. image:: /_images/components/console/cursor.gif  | 
 | 17 | + :align: center  | 
 | 18 | + | 
 | 19 | +.. code-block:: php  | 
 | 20 | +
  | 
 | 21 | + // src/Command/MyCommand.php  | 
 | 22 | + namespace App\Command;  | 
 | 23 | +
  | 
 | 24 | + use Symfony\Component\Console\Command\Command;  | 
 | 25 | + use Symfony\Component\Console\Cursor;  | 
 | 26 | + use Symfony\Component\Console\Input\InputInterface;  | 
 | 27 | + use Symfony\Component\Console\Output\OutputInterface;  | 
 | 28 | +
  | 
 | 29 | + class MyCommand extends Command  | 
 | 30 | + {  | 
 | 31 | + // ...  | 
 | 32 | +
  | 
 | 33 | + public function execute(InputInterface $input, OutputInterface $output): int  | 
 | 34 | + {  | 
 | 35 | + // ...  | 
 | 36 | +
  | 
 | 37 | + $cursor = new Cursor($output);  | 
 | 38 | +
  | 
 | 39 | + // moves the cursor to a specific column (1st argument) and  | 
 | 40 | + // row (2nd argument) position  | 
 | 41 | + $cursor->moveToPosition(7, 11);  | 
 | 42 | +
  | 
 | 43 | + // and write text on this position using the output  | 
 | 44 | + $output->write('My text');  | 
 | 45 | +
  | 
 | 46 | + // ...  | 
 | 47 | + }  | 
 | 48 | + }  | 
 | 49 | +
  | 
 | 50 | +Using the cursor  | 
 | 51 | +----------------  | 
 | 52 | + | 
 | 53 | +Moving the cursor  | 
 | 54 | +.................  | 
 | 55 | + | 
 | 56 | +There are fews methods to control moving the command cursor::  | 
 | 57 | + | 
 | 58 | + // moves the cursor 1 line up from its current position  | 
 | 59 | + $cursor->moveUp();  | 
 | 60 | + | 
 | 61 | + // moves the cursor 3 lines up from its current position  | 
 | 62 | + $cursor->moveUp(3);  | 
 | 63 | + | 
 | 64 | + // same for down  | 
 | 65 | + $cursor->moveDown();  | 
 | 66 | + | 
 | 67 | + // moves the cursor 1 column right from its current position  | 
 | 68 | + $cursor->moveRight();  | 
 | 69 | + | 
 | 70 | + // moves the cursor 3 columns right from its current position  | 
 | 71 | + $cursor->moveRight(3);  | 
 | 72 | + | 
 | 73 | + // same for left  | 
 | 74 | + $cursor->moveLeft();  | 
 | 75 | + | 
 | 76 | + // move the cursor to a specific (column, row) position from the  | 
 | 77 | + // top-left position of the terminal  | 
 | 78 | + $cursor->moveToPosition(7, 11);  | 
 | 79 | + | 
 | 80 | +You can get the current command's cursor position by using::  | 
 | 81 | + | 
 | 82 | + $position = $cursor->getCurrentPosition();  | 
 | 83 | + // $position[0] // columns (aka x coordinate)  | 
 | 84 | + // $position[1] // rows (aka y coordinate)  | 
 | 85 | + | 
 | 86 | +Clearing output  | 
 | 87 | +...............  | 
 | 88 | + | 
 | 89 | +The cursor can also clear some output on the screen::  | 
 | 90 | + | 
 | 91 | + // clears all the output from the current line  | 
 | 92 | + $cursor->clearLine();  | 
 | 93 | + | 
 | 94 | + // clears all the output from the current line after the current position  | 
 | 95 | + $cursor->clearLineAfter();  | 
 | 96 | + | 
 | 97 | + // clears all the output from the cursors' current position to the end of the screen  | 
 | 98 | + $cursor->clearOutput();  | 
 | 99 | + | 
 | 100 | + // clears the entire screen  | 
 | 101 | + $cursor->clearScreen();  | 
 | 102 | + | 
 | 103 | +You also can leverage the :method:`Symfony\\Component\\Console\\Cursor::show`  | 
 | 104 | +and :method:`Symfony\\Component\\Console\\Cursor::hide` methods on the cursor.  | 
0 commit comments