Skip to content

[Console] Document console cursor #15270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added _images/components/console/cursor.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions components/console/helpers/cursor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
.. index::
single: Console Helpers; Cursor Helper

Cursor Helper
=============

.. versionadded:: 5.1

The :class:`Symfony\\Component\\Console\\Cursor`
class was introduced in Symfony 5.1.

The :class:`Symfony\\Component\\Console\\Cursor` allows you to change the
cursor position in a console command. This allows you to write on any position
of the output:

.. image:: /_images/components/console/cursor.gif
:align: center


.. code-block:: php

// src/Commande/MyCommand.php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Cursor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{
// ...

public function execute(InputInterface $input, OutputInterface $output)
{
// ...

$cursor = new Cursor($output);

// moves the cursor to a specific column and row position
$cursor->moveToPosition(7, 11);

// and write text on this position using the output
$output->write('My text');

// ...
}
}

Using the cursor
----------------

Moving the cursor
.................

There are fews methods to control moving the command cursor::

// moves the cursor 1 line up from its current position
$cursor->moveUp();

// moves the cursor 3 lines up from its current position
$cursor->moveUp(3);

// same for down
$cursor->moveDown();

// moves the cursor 1 column right from its current position
$cursor->moveRight();

// moves the cursor 3 columns right from its current position
$cursor->moveRight(3);

// same for left
$cursor->moveLeft();

// move the cursor to a specific position from its current position
$cursor->moveToPosition(7, 11);
Comment on lines +74 to +75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used this feature, but I think we have to clearify this a bit. This means 7 lines down, and 11 columns to the right? And I can use negative numbers to achieve the opposite?

Or does this expect a coordinate?

Copy link
Contributor Author

@noniagriconomie noniagriconomie May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wouterj it means 7 cols right, 11 rows down from console init cursor
the cursor always moves position from this 0:0 position, thus need an x:y (here positive values) from the init yes


You can get the current command's cursor position by using::

$position = $cursor->getCurrentPosition();
// $position[0] // columns (aka x coordinate)
// $position[1] // rows (aka y coordinate)

Clearing output
...............

The cursor can also clear some output on the screen::

// clears all the output from the current line
$cursor->clearLine();

// clears all the output from the current line after the current position
$cursor->clearLineAfter();

// clears all the output from the cursors' current position to the end of the screen
$cursor->clearOutput();

// clears the entire screen
$cursor->clearScreen();

You also can leverage the :method:`Symfony\\Component\\Console\\Cursor::show`
and :method:`Symfony\\Component\\Console\\Cursor::hide` methods on the cursor.
1 change: 1 addition & 0 deletions components/console/helpers/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The Console Helpers
questionhelper
table
debug_formatter
cursor

The Console component comes with some useful helpers. These helpers contain
functions to ease some common tasks.
Expand Down
1 change: 1 addition & 0 deletions components/console/helpers/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
* :doc:`/components/console/helpers/questionhelper`
* :doc:`/components/console/helpers/table`
* :doc:`/components/console/helpers/debug_formatter`
* :doc:`/components/console/helpers/cursor`
2 changes: 1 addition & 1 deletion components/console/helpers/progressbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ placeholder before displaying the progress bar::
// 0/100 -- Start

$progressBar->setMessage('Task is in progress...');
$progressBar->advance();
$progressBar->advance();
// 1/100 -- Task is in progress...

Messages can be combined with custom placeholders too. In this example, the
Expand Down
1 change: 1 addition & 0 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,6 @@ tools capable of helping you with different tasks:
* :doc:`/components/console/helpers/table`: displays tabular data as a table
* :doc:`/components/console/helpers/debug_formatter`: provides functions to
output debug information when running an external program
* :doc:`/components/console/helpers/cursor`: allows to manipulate the cursor in the terminal

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