Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 5.1: Cursor control

Contributed by
Pierre du Plessis
in #27444.

Symfony Console component provides lots of utilities to render contents in the command console, such as progress bars, tables with all kinds of styles, interactive questions, etc.

In Symfony 4.1 we even introduced output sections allowing to update different parts of the output independently. In Symfony 5.1 we've introduced another advanced feature which will allow you control the cursor.

Thanks to the new Symfony\Component\Console\Cursor class and its expressive API, you can show/hide the cursor, move it up/down/left/right, clear parts of the output, etc.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Cursor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SomeCommand extends Command
{
    protected static $defaultName = 'app:some-command';

    // ...

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

        $cursor = new Cursor($output);

        // argument for left/right is "number of columns" (1 by default)
        // argument for top/bottom is "number of rows" (1 by default)
        $cursor->moveUp(2)->moveRight();
        $cursor->moveDown();

        // move to an arbitrary (column, row) position
        $cursor->moveToPosition(7, 15);

        // you can show/hide the cursor, save/restore its position, etc.
        $cursor->savePosition()->hide();
    }
}

In addition to these methods, the new Cursor class provides several utilities to clean the output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// clears the entire line where the cursor is at
$cursor->clearLine();

// clears the contents of the current line starting from the cursor 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();
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

Finally we are able to code Tetris using Symfony Console 🎉
Example's Cursor constructor is missing OutputInterface argument.
Paweł good catch! It's fixed now. Thanks.
Is there some game project based on Console component known?
@David Romaní I think there is not yet, but with this feature, you might expect some to come :D
@David Romaní I started a chess game using Symfony Console component. It's still work in progress, but you can see it here https://github.com/marydn/shogi =)
Congratulations @marydn, I'll have a look!
I could not resist playing around with the cursor. The result is https://packagist.org/packages/dbu/snake-bundle

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.