diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 1381d5f394..49ef6224f7 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -808,29 +808,29 @@ class Application $title = sprintf(' [%s] ', get_class($e)); $len = $strlen($title); $width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; + $formatter = $output->getFormatter(); $lines = array(); foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) { foreach (str_split($line, $width - 4) as $line) { - $lines[] = sprintf(' %s ', $line); - $len = max($strlen($line) + 4, $len); + // pre-format lines to get the right string length + $lineLength = $strlen(preg_replace('/\[[^m]*m/', '', $formatter->format($line))) + 4; + $lines[] = array($line, $lineLength); + + $len = max($lineLength, $len); } } - $messages = array(str_repeat(' ', $len), $title.str_repeat(' ', max(0, $len - $strlen($title)))); - + $messages = array('', ''); + $messages[] = $emptyLine = $formatter->format(sprintf('%s', str_repeat(' ', $len))); + $messages[] = $formatter->format(sprintf('%s%s', $title, str_repeat(' ', max(0, $len - $strlen($title))))); foreach ($lines as $line) { - $messages[] = $line.str_repeat(' ', $len - $strlen($line)); + $messages[] = $formatter->format(sprintf(' %s %s', $line[0], str_repeat(' ', $len - $line[1]))); } + $messages[] = $emptyLine; + $messages[] = ''; + $messages[] = ''; - $messages[] = str_repeat(' ', $len); - - $output->writeln(""); - $output->writeln(""); - foreach ($messages as $message) { - $output->writeln(''.$message.''); - } - $output->writeln(""); - $output->writeln(""); + $output->writeln($messages, OutputInterface::OUTPUT_RAW); if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { $output->writeln('Exception trace:'); diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatter.php b/src/Symfony/Component/Console/Formatter/OutputFormatter.php index f3c3298866..36721601ed 100644 --- a/src/Symfony/Component/Console/Formatter/OutputFormatter.php +++ b/src/Symfony/Component/Console/Formatter/OutputFormatter.php @@ -23,7 +23,7 @@ class OutputFormatter implements OutputFormatterInterface /** * The pattern to phrase the format. */ - const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>((?: [^<\\\\]+ | (?!<(?:/?[a-z]|/>)). | .(?<=\\\\<) )*)#isx'; + const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]*)?>((?: [^<\\\\]+ | (?!<(?:/?[a-z]|/>)). | .(?<=\\\\<) )*)#isx'; private $decorated; private $styles = array(); @@ -163,6 +163,8 @@ class OutputFormatter implements OutputFormatterInterface /** * Replaces style of the output. * + * All escaped tags and tags that reference unknown styles are kept as is. + * * @param array $match * * @return string The replaced style diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index f78d8de107..7f73aa1a4d 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -429,6 +429,9 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $tester->run(array('command' => 'foo3:bar'), array('decorated' => false)); $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalizeLineBreaks($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions'); + $tester->run(array('command' => 'foo3:bar'), array('decorated' => true)); + $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions'); + $application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth')); $application->setAutoExit(false); $application->expects($this->any()) diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php b/src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php index 7349bc355e..43a35076e3 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php +++ b/src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php @@ -17,9 +17,13 @@ class Foo3Command extends Command protected function execute(InputInterface $input, OutputInterface $output) { try { - throw new \Exception("First exception"); + try { + throw new \Exception("First exception

this is html

"); + } catch (\Exception $e) { + throw new \Exception("Second exception comment", 0, $e); + } } catch (\Exception $e) { - throw new \Exception("Second exception", 0, $e); + throw new \Exception("Third exception comment", 0, $e); } } } diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt index c639924238..72a72867f3 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt @@ -1,17 +1,25 @@ - - [Exception] - Second exception - + + [Exception] + Third exception comment + - - [Exception] - First exception - + + [Exception] + Second exception comment + + + + + + + [Exception] + First exception

this is html

+ foo3:bar diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt new file mode 100644 index 0000000000..eb4181c2b4 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt @@ -0,0 +1,27 @@ + + +  + [Exception]  + Third exception comment  +  + + + + +  + [Exception]  + Second exception comment  +  + + + + +  + [Exception]  + First exception 

this is html

 +  + + +foo3:bar + + diff --git a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php index 497630e87c..08bd02e852 100644 --- a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php +++ b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php @@ -113,7 +113,10 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($style, $formatter->getStyle('test')); $this->assertNotEquals($style, $formatter->getStyle('info')); - $this->assertEquals("\033[34;47msome custom msg\033[0m", $formatter->format('some custom msg')); + $style = new OutputFormatterStyle('blue', 'white'); + $formatter->setStyle('b', $style); + + $this->assertEquals("\033[34;47msome \033[0m\033[34;47mcustom\033[0m\033[34;47m msg\033[0m", $formatter->format('some custom msg')); } public function testRedefineStyle() @@ -137,7 +140,7 @@ class FormatterStyleTest extends \PHPUnit_Framework_TestCase public function testNonStyleTag() { $formatter = new OutputFormatter(true); - $this->assertEquals("\033[32msome \033[0m\033[32m styled\033[0m", $formatter->format('some styled')); + $this->assertEquals("\033[32msome \033[0m\033[32m styled \033[0m\033[32m

single-char tag\033[0m\033[32m

\033[0m", $formatter->format('some styled

single-char tag

')); } public function testNotDecoratedFormatter()