Skip to content

Commit c159fee

Browse files
committed
Merge remote-tracking branch 'upstream/5.2' into 5.2
* upstream/5.2: [#15270] Minor fixes [Console] Document console cursor
2 parents 71e656f + 7565926 commit c159fee

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

.doctor-rst.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,4 @@ whitelist:
103103
- '.. code-block:: twig'
104104
- '.. versionadded:: 3.6' # MonologBundle
105105
- 'End to End Tests (E2E)'
106+
- '.. code-block:: php'

_images/components/console/cursor.gif

63.4 KB
Loading

components/console/helpers/cursor.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.

components/console/helpers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The Console Helpers
1313
questionhelper
1414
table
1515
debug_formatter
16+
cursor
1617

1718
The Console component comes with some useful helpers. These helpers contain
1819
functions to ease some common tasks.

components/console/helpers/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* :doc:`/components/console/helpers/questionhelper`
55
* :doc:`/components/console/helpers/table`
66
* :doc:`/components/console/helpers/debug_formatter`
7+
* :doc:`/components/console/helpers/cursor`

components/console/helpers/progressbar.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ placeholder before displaying the progress bar::
349349
// 0/100 -- Start
350350

351351
$progressBar->setMessage('Task is in progress...');
352-
$progressBar->advance();
352+
$progressBar->advance();
353353
// 1/100 -- Task is in progress...
354354

355355
Messages can be combined with custom placeholders too. In this example, the

console.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,5 +446,6 @@ tools capable of helping you with different tasks:
446446
* :doc:`/components/console/helpers/table`: displays tabular data as a table
447447
* :doc:`/components/console/helpers/debug_formatter`: provides functions to
448448
output debug information when running an external program
449+
* :doc:`/components/console/helpers/cursor`: allows to manipulate the cursor in the terminal
449450

450451
.. _`exit status`: https://en.wikipedia.org/wiki/Exit_status

0 commit comments

Comments
 (0)