symfony/symfony — pull request #3559 by jfsimon (2012-03-11 12:46:52+00:00)
Title: [Console] Added style helper.

Bug fix: almost
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

When outputing styled text in the console, you sometimes face to a confusing behavior: style tags cannot be nested. If tou try something like `Hello world!`, the trailing `!` will not be styled.

This helper comes with a flatten method which resolve this problem by -for instance- turning `Hello world!` into `Hello world!`.

As-is, it manages expression matching `##is`, which can be annoying if you wish to output some XML tags. So I added a tag validation capability to only match style tags (ie. `#<([fb]g=[a-z]+?;?)+>#is` expressions, or known style names if a formatter is provided).

I also added convenience methods for removing style tags and highlighting expressions from text.

A small example within a command class:

protected function execute(InputInterface $input, OutputInterface $output)
{
$style = $this
->getHelperSet()
->get('style')
->enableValidation($output->getFormatter());

$output->writeln($style->flatten('Hello world!'));
}