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