@@ -9,30 +9,91 @@ Cursor Helper
9
9
The :class: `Symfony\\ Component\\ Console\\ Cursor `
10
10
class was introduced in Symfony 5.1.
11
11
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:
14
15
15
16
.. image :: /_images/components/console/cursor.gif
16
17
:align: center
17
18
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
+
18
46
Using the cursor
19
47
----------------
20
48
21
- The cursor defines several functions and can be used as follow::
49
+ Moving the cursor
50
+ ................
22
51
23
- use Symfony\Component\Console\Cursor;
24
- use Symfony\Component\Console\Output\OutputInterface;
52
+ There are fews methods to control moving the command cursor::
25
53
26
- // creates a new cursor
27
- $cursor = new Cursor($output );
54
+ // moves the cursor 1 line up from its current position
55
+ $cursor->moveUp( );
28
56
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 );
31
59
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
33
64
$cursor->moveRight();
34
65
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
36
87
$cursor->clearLine();
37
88
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