Skip to content

Commit

Permalink
[Console] fixed exception rendering when nested styles
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 17, 2013
1 parent 1f88a28 commit c8d0342
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
29 changes: 14 additions & 15 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -23,7 +23,6 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\DialogHelper;
Expand Down Expand Up @@ -809,29 +808,29 @@ public function renderException($e, $output)
$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('<error>%s</error>', str_repeat(' ', $len)));
$messages[] = $formatter->format(sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $strlen($title)))));
foreach ($lines as $line) {
$messages[] = $line.str_repeat(' ', $len - $strlen($line));
$messages[] = $formatter->format(sprintf('<error> %s %s</error>', $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('<error>'.OutputFormatter::escape($message).'</error>');
}
$output->writeln("");
$output->writeln("");
$output->writeln($messages, OutputInterface::OUTPUT_RAW);

if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) {
$output->writeln('<comment>Exception trace:</comment>');
Expand Down
Expand Up @@ -20,7 +20,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
try {
throw new \Exception("First exception <p>this is html</p>");
} catch (\Exception $e) {
throw new \Exception("Second exception <comment>comment<comment>", 0, $e);
throw new \Exception("Second exception <comment>comment</comment>", 0, $e);
}
} catch (\Exception $e) {
throw new \Exception("Third exception <fg=blue;bg=red>comment</>", 0, $e);
Expand Down
@@ -1,17 +1,17 @@


[Exception]
Third exception <fg=blue;bg=red>comment</>

[Exception]
Third exception comment





[Exception]
Second exception <comment>comment<comment>

[Exception]
Second exception comment




Expand Down
@@ -1,24 +1,24 @@


 
 [Exception] 
 Third exception <fg=blue;bg=red>comment</> 
 
 
 [Exception] 
 Third exception comment 
 




 
 [Exception] 
 Second exception <comment>comment<comment> 
 
 
 [Exception] 
 Second exception comment 
 




 
 [Exception] 
 First exception <p>this is html</p> 
 First exception <p>this is html</p> 
 


Expand Down

0 comments on commit c8d0342

Please sign in to comment.