New in Symfony 6.2: Console improvements
November 25, 2022 • Published by Javier Eguiluz
Symfony 6.2 is backed by:
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The Console component is the most downloaded non-polyfill component (+530 million downloads as of November 2022) and the component with most dependents overall (nearly 10,000 public projects depend on it). In Symfony 6.2 we added some nice new features to it.
Show Commands When Running Namespace
When running only the namespace instead of the full command name, Symfony displays an error message:
1 2 3 4 5 6 7
$ bin/console debug
Command "debug" is not defined. Did you mean one of these?
debug:autowiring
debug:config
debug:container
[...]
In Symfony 6.2 we're improving this to list instead all the commands defined under the given namespace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ bin/console debug
Symfony 6.2.0 (env: dev, debug: true) #StandWithUkraine https://sf.to/ukraine
Usage:
command [options] [arguments]
[...]
Available commands for the "debug" namespace:
debug:autowiring List classes/interfaces you can use for autowiring
debug:config Dump the current configuration for an extension
debug:container Display current services for an application
debug:dotenv Lists all dotenv files with variables and values
debug:event-dispatcher Display configured listeners for an application
debug:router Display current routes for an application
Resuming Progress Bars
Symfony 6.2 adds the option to resume a progress bar so it doesn't always start
from 0
. This is useful not only to resume long standing tasks but also to
show the progress of a complex task with optional steps that can be skipped:
1 2 3 4 5 6 7 8
use Symfony\Component\Console\Helper\ProgressBar;
// create a new progress bar (length = 100 units)
$progressBar = new ProgressBar($output, 100);
// pass the new optional second argument to display the progress bar
// starting at some point different from 0 (in this example = 25)
$progressBar->start(null, 25);
Improved Color Support
In Symfony 5.2 we introduced true colors in the console to support Ansi24 (24-bit colors) mode. In Symfony 6.2 we're improving color support by adding a new Ansi8 (256-color) mode for terminals that don't support true colors.
The best of all is that you don't have to change anything in your code. You can
still use 24-bit colors and Symfony will transform them to the nearest 8-bit
colors if the terminal doesn't support them (e.g. #c0392b
(24-bit) is degraded
to #d75f5f
in 8-bit color terminals and to red
in 4-bit color terminals).
Lastly, we've introduced a setColorMode(?AnsiColorMode $colorMode)
method
in the main Terminal
class so you can force a color mode in the terminal
(it overrides any env var defining the color mode).
Zsh Shell Autocompletion
In Symfony 5.4 we introduced console autocompletion so you can press the TAB
key to see a list of contextual suggestions for commands, arguments, options, etc.
Initially it only worked on Bash shells, but in Symfony 6.2 we're also adding
support for Zsh shell autocompletion.
Limiting the Height of Console Sections
Console introduced output sections in Symfony 4.1. They allow to define different output regions that you can manage independently. In Symfony 6.2 we're adding a feature to limit the height of any section:
1 2 3 4 5 6 7 8 9 10 11 12 13
class MyCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
// ...
$debugMessages = $output->section();
$debugMessages->setMaxHeight(3);
// ...
}
}
When the contents overflow the given number of rows, the new contents replace the existing contents. This is very useful to show verbose output in long running processes, without taking over the complete terminal buffer:
In case you missed it, your company can sponsor the Console component or any other component published by Symfony. Check out the details of our Symfony Sponsoring Program.
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 are closed.
To ensure that comments stay relevant, they are closed for old posts.
Thanks for Zsh Shell Autocompletion 👌😁