New in Symfony 2.8: Console improvements

The Symfony Console component is our second most popular component, with more than 1 million monthly downloads and more than 2,300 projects depending on it. In Symfony 2.8 we extended its capabilities with some new features.

Allowed to apply styles to table columns

Contributed by
Max Grigorian
in #14044.

Symfony 2.8 allows to define a different visual style for each table column. First, instantiate the TableStyle class to define the style features:

1
2
3
4
use Symfony\Component\Console\Helper\TableStyle;

$rightAligned = new TableStyle();
$rightAligned->setPadType(STR_PAD_LEFT);

Then, apply the custom style to any table column through its column numeric index:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use Symfony\Component\Console\Helper\Table;

$table = new Table($output);
$table->setHeaders(['#', 'Path', 'Size']);

$table->setColumnStyle(2, $rightAligned);

// ...

$table->render();

Now, when rendering the table, the contents of the third column (numeric index = 2) will be right aligned:

1
2
3
4
5
6
7
8
+---+---------------------+---------+
| # | Path                |    Size |
+---+---------------------+---------+
| 1 | autoload.php        |     183 |
| 2 | ApplicationTest.php | 247,794 |
| 3 | CommandTest.php     |  14,965 |
| 4 | ListCommandTest.php |   2,369 |
+---+---------------------+---------+

Made exceptions visible in quiet mode

Contributed by
Jordi Boggiano
in #15772.

In some environments it's common to run console commands with the --quiet option to avoid displaying any output. The problem is that this quiet mode also suppress the output when an exception occurs.

In Symfony 2.8, the behavior of the "quiet verbosity mode" has changed to always display the exception details when such an error occurs. This will make your life as a developer easier and it aligns with our Developer Experience commitment.

Added a progress indicator helper

Contributed by
Kevin Bond
in #16409.

The progress bar helper allows to create cool animated indicators of the progress of some task. They are useful when you know beforehand when the task will finish, as in the Symfony Installer download progress bar.

However, it's common to execute tasks where you can't determine in advance when they'll be completed. That's why we've added a new progress indicator helper which displays a spinning bar while the task is running:

In addition to the spinning bar, you can output messages while the progress indicator is still active:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use Symfony\Component\Console\Helper\ProgressIndicator;

$progress = new ProgressIndicator($output);
$progress->start('Starting...');

// do something ...
$progress->advance();

// do something ...
$progress->setMessage('Just started...');

// do something ...
$progress->setMessage('Half way...');

// do something ...
$progress->setMessage('Almost Done...');

// do something ...
$progress->finish('Done.');

Another nice feature of this progress indicator is that it gracefully degrades when the system doesn't support ANSI codes:

Comments

super awesome, i love it!
Amazing, thanks for the hard work guys!
"Now, when rendering the table, the contents of the third column (numeric index = 2) will be left aligned:"

Except, in the example, the third column is RIGHT aligned. The SECOND column is left aligned though... maybe the array is not zero-based?
@Craig I've fixed the original example. The style is now called "rightAligned".
@Javier Maybe "STR_PAD_LEFT" should be "STR_PAD_RIGHT"?
It's ok, I was wrong, it's pad, not align!
Nice improvement the progress, users'll be happy with this ;)
Nice but waiting for the news title: "Symfony 2.8.0 released" :P
Nice
I liked of the progress indicator, great work.

Comments are closed.

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