New in Symfony 4.1: Advanced Console Output

Contributed by
Pierre du Plessis
in #24363.

The Console component is the second most popular Symfony component (more than 82 million downloads!) and it's packed with amazing features. In Symfony 4.1 we improved it even more with a feature to create and manipulate multiple output sections.

Currently, displaying information in a command console is a pretty basic operation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class MyCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Display some information...');

        // ...

        $output->writeln('Display more information...');
    }
}

In Symfony 4.1 you'll be able to display information, overwrite it, delete part of it and update different parts of the output simultaneously. The new operations are based on "output sections" which are independently managed regions of the console output:

1
2
3
4
5
6
7
8
9
class MyCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $section = $output->section();
        $section->writeln('Display some information...');
        // ...
    }
}

Overwriting output contents

The new overwrite() method deletes all section contents and replaces them with the given contents:

1
2
3
4
5
6
7
$section = $output->section();
$section->writeln('Downloading the file...');
// ...
$section->overwrite('Uncompressing the file...');
// ...
$section->overwrite('Copying the contents...');
// ...

Deleting output contents

The new clear(int $numLines) method deletes the last $numLines of the section (or all contents if no argument is provided):

1
2
3
4
5
6
7
8
$section = $output->section();
$section->writeln('Welcome to the installation Process!');
$section->writeln('Downloading the file...');
$section->writeln('Uncompressing the file...');
$section->writeln('Copying the contents...');
// ...
$section->clear(3);
$section->writeln('The installation is complete!');

Appending rows to rendered tables

In previous Symfony versions, displaying a table required to know all its rows and columns. However, the new appendRow() method, which works as the existing addRow() method, allows to add new rows for already displayed tables:

1
2
3
4
5
6
7
8
9
$section = $output->section();
$table = new Table($section);

$table->addRow(['Row 1']);
// display the table with the known contents
$table->render();

// add a new row with new contents to the already displayed table
$table->appendRow(['Row 2']);

Managing multiple outputs

The most interesting new feature is that you can create as many output sections as needed and manage them independently. The following example displays a progress bar while a table is being updated and when the command finishes, the progress bar is removed and the table is maintained:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$section1 = $output->section();
$section2 = $output->section();

$progress = new ProgressBar($section1);
$progress->start(5);

$table = new Table($section2);
$table->addRow(['Row 1']);
$table->render();

foreach ($rowsToProcess as $i => $row) {
    $table->appendRow(['Row '.$i++]);
    $progress->advance();
    // ...
}

$progress->finish();
$section1->clear();

Comments

Many thanks!
Awesome features. Thanks a lot!
This is really great, can't wait to fully use it ! Thanks everyone
Wow, super cool!
Aaah great, I've been waiting for this for a long time:)
nice !
Nice feature. Thank you everyone.
Great news!
Great !
I love it!
Wonderful! Thanks!
This is great, thanks!
I'll really use this one, thank you Pierre!
Would it be possible to add a gif to show how it looks like? (or at least in the documentation)
@Philippe Villiers there are gifs illustrating the feature on the PR page. There's a link to it in the "contributed by" block at the top of this blog post.
Exactly what I was looking for. Awesome improvements!
Now let's create a "Snake" game in the command-line with the help of Symfony :D
Pretty cool indeed ;)
Nice, I hope it will be added to Symfony\Component\Console\Style\SymfonyStyle too
Nice feature. Thank you everyone !
Login with SensioLabsConnect to post a comment