Skip to content

Commit 74af6d8

Browse files
Reviews and continuation
1 parent ba7a0f2 commit 74af6d8

File tree

1 file changed

+73
-12
lines changed

1 file changed

+73
-12
lines changed

components/console/helpers/cursor.rst

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,91 @@ Cursor Helper
99
The :class:`Symfony\\Component\\Console\\Cursor`
1010
class was introduced in Symfony 5.1.
1111

12-
The :class:`Symfony\\Component\\Console\\Cursor` allows you to manipulate
13-
the cursor in a console command like this:
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:
1415

1516
.. image:: /_images/components/console/cursor.gif
1617
:align: center
1718

19+
20+
// src/Commande/MyCommand.php
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Cursor;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
class MyCommand extends Command
27+
{
28+
// ...
29+
30+
public function execute(InputInterface $input, OutputInterface $output)
31+
{
32+
// ...
33+
34+
$cursor = new Cursor($output);
35+
36+
// moves the cursor to a specific column and row position
37+
$cursor->moveToPosition(7, 11);
38+
39+
// and write text on this position using the output
40+
$output->write('My text');
41+
42+
// ...
43+
}
44+
}
45+
1846
Using the cursor
1947
----------------
2048

21-
The cursor defines several functions and can be used as follow::
49+
Moving the cursor
50+
................
2251

23-
use Symfony\Component\Console\Cursor;
24-
use Symfony\Component\Console\Output\OutputInterface;
52+
There are fews methods to control moving the command cursor::
2553

26-
// creates a new cursor
27-
$cursor = new Cursor($output);
54+
// moves the cursor 1 line up from its current position
55+
$cursor->moveUp();
2856

29-
// moves the cursor to column and row
30-
$cursor->moveToPosition(7, 11);
57+
// moves the cursor 3 lines up from its current position
58+
$cursor->moveUp(3);
3159

32-
// moves the cursor to the right
60+
// same for down
61+
$cursor->moveDown();
62+
63+
// moves the cursor 1 column right from its current position
3364
$cursor->moveRight();
3465

35-
// clear the current cursor line
66+
// moves the cursor 3 columns right from its current position
67+
$cursor->moveRight(3);
68+
69+
// same for left
70+
$cursor->moveLeft();
71+
72+
// move the cursor to a specific position from its current position
73+
$cursor->moveToPosition(7, 11);
74+
75+
You can get the current command's cursor position by using::
76+
77+
$position = $cursor->getCurrentPosition();
78+
// $position[0] // columns (aka x coordinate)
79+
// $position[1] // rows (aka y coordinate)
80+
81+
Clearing output
82+
................
83+
84+
The cursor can also clear some output on the screen::
85+
86+
// clears all the output from the current line
3687
$cursor->clearLine();
3788

38-
// [...] read the class to see all the features
89+
// clears all the output from the current line after the current position
90+
$cursor->clearLineAfter();
91+
92+
// clears all the output from the cursors' current position to the end of the screen
93+
$cursor->clearOutput();
94+
95+
// clears the entire screen
96+
$cursor->clearScreen();
97+
98+
You also can leverage the :method:`Symfony\\Component\\Console\\Cursor::show`
99+
and :method:`Symfony\\Component\\Console\\Cursor::hide` methods on the cursor.

0 commit comments

Comments
 (0)