Description
Q | A |
---|---|
Bug report? | yes |
Feature request? | yes |
Symfony version | 2.8.19 though probably earlier |
When an exception is generated by Symfony Console, the output is in a nice big red box.
throw new RuntimeException(
sprintf(
'Unable to locate the src directory within <fg=yellow;bg=red>%s</fg=yellow;bg=red>.'.PHP_EOL.
'Try running with <fg=yellow;bg=red>--ignore-checkers</fg=yellow;bg=red>.',
$input->getArgument('phpmaker-installation')
)
);
But, unfortunately, if you put in style tags, they are rendered as literals.
If you override \Symfony\Component\Console\Output\OutputInterface::write()
and unescape the tags, this will provide the output colouring, but not the correct lengths.
class MyOutput extends \Symfony\Component\Console\Output\Output
{
/**
* @inheritdoc
*
* Also removes the escaping of <fg> and <bg> tags.
*/
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
{
parent::write(
array_map(
function ($message) {
return preg_replace('`\\\(?=</?[fb]g=)`', '', $message);
},
(array) $messages
),
$newline,
$options
);
}
}
The width of the block is determined by \Symfony\Component\Console\Application::stringWidth()
.
If this method was to remove the style tags before determining the length, then the length would/should be correct.
I'm aware that not all uses of '<xxxx>'
will be style tags. Some may be literal and therefore escaping of the '<'
(which is performed by \Symfony\Component\Console\Application::renderException()
when it calls \Symfony\Component\Console\Formatter\OutputFormatter::escape()
).
One idea on how to solve this would be to have an internal OutputFormatter that essentially removes known style tags. This formatter could wrap the string being passed to \Symfony\Component\Console\Application::stringWidth()
, thus providing the correct string length.
The example code above doesn't take into account the other formats of the style tags (named styles).
Also, the \Symfony\Component\Console\Formatter\OutputFormatter::escape()
method would need to ignore recognised style tags.
Happy to work on this, but any pointers, things I've missed, etc would be appreciated.