Symfony News

New in Symfony 4.1: Advanced Console Output

Pierre du Plessis

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();

Be trained by Symfony experts - 2018-04-23 Lyon - 2018-04-23 Lyon - 2018-04-25 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook