How to Color and Style the Console Output

How to Color and Style the Console Output

By using colors in the command output, you can distinguish different types of output (e.g. important messages, titles, comments, etc.).

Note

By default, the Windows command console doesn’t support output coloring. The Console component disables output coloring for Windows systems, but if your commands invoke other scripts which emit color sequences, they will be wrongly displayed as raw escape characters. Install the Cmder, ConEmu, ANSICON, Mintty (used by default in GitBash and Cygwin) or Hyper free applications to add coloring support to your Windows command console.

Using Color Styles

Whenever you output text, you can surround the text with tags to color its output. For example:

  1. // green text
  2. $output->writeln('<info>foo</info>');
  3. // yellow text
  4. $output->writeln('<comment>foo</comment>');
  5. // black text on a cyan background
  6. $output->writeln('<question>foo</question>');
  7. // white text on a red background
  8. $output->writeln('<error>foo</error>');

The closing tag can be replaced by </>, which revokes all formatting options established by the last opened tag.

It is possible to define your own styles using the Symfony\Component\Console\Formatter\OutputFormatterStyle class:

  1. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  2. // ...
  3. $outputStyle = new OutputFormatterStyle('red', 'yellow', ['bold', 'blink']);
  4. $output->getFormatter()->setStyle('fire', $outputStyle);
  5. $output->writeln('<fire>foo</>');

Available foreground and background colors are: black, red, green, yellow, blue, magenta, cyan and white.

And available options are: bold, underscore, blink, reverse (enables the “reverse video” mode where the background and foreground colors are swapped) and conceal (sets the foreground color to transparent, making the typed text invisible - although it can be selected and copied; this option is commonly used when asking the user to type sensitive information).

You can also set these colors and options directly inside the tag name:

  1. // green text
  2. $output->writeln('<fg=green>foo</>');
  3. // black text on a cyan background
  4. $output->writeln('<fg=black;bg=cyan>foo</>');
  5. // bold text on a yellow background
  6. $output->writeln('<bg=yellow;options=bold>foo</>');
  7. // bold text with underscore
  8. $output->writeln('<options=bold,underscore>foo</>');

Note

If you need to render a tag literally, escape it with a backslash: \<info> or use the [escape()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Formatter/OutputFormatter.php "Symfony\Component\Console\Formatter\OutputFormatter::escape()") method to escape all the tags included in the given string.

New in version 4.3: The feature to display clickable links was introduced in Symfony 4.3.

Commands can use the special <href> tag to display links similar to the <a> elements of web pages:

  1. $output->writeln('<href=https://symfony.com>Symfony Homepage</>');

If your terminal belongs to the list of terminal emulators that support links you can click on the “Symfony Homepage” text to open its URL in your default browser. Otherwise, you’ll see “Symfony Homepage” as regular text and the URL will be lost.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.