Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8d0342

Browse files
committed
[Console] fixed exception rendering when nested styles
1 parent 1f88a28 commit c8d0342

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Console\Command\Command;
2424
use Symfony\Component\Console\Command\HelpCommand;
2525
use Symfony\Component\Console\Command\ListCommand;
26-
use Symfony\Component\Console\Formatter\OutputFormatter;
2726
use Symfony\Component\Console\Helper\HelperSet;
2827
use Symfony\Component\Console\Helper\FormatterHelper;
2928
use Symfony\Component\Console\Helper\DialogHelper;
@@ -809,29 +808,29 @@ public function renderException($e, $output)
809808
$title = sprintf(' [%s] ', get_class($e));
810809
$len = $strlen($title);
811810
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
811+
$formatter = $output->getFormatter();
812812
$lines = array();
813813
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
814814
foreach (str_split($line, $width - 4) as $line) {
815-
$lines[] = sprintf(' %s ', $line);
816-
$len = max($strlen($line) + 4, $len);
815+
// pre-format lines to get the right string length
816+
$lineLength = $strlen(preg_replace('/\[[^m]*m/', '', $formatter->format($line))) + 4;
817+
$lines[] = array($line, $lineLength);
818+
819+
$len = max($lineLength, $len);
817820
}
818821
}
819822

820-
$messages = array(str_repeat(' ', $len), $title.str_repeat(' ', max(0, $len - $strlen($title))));
821-
823+
$messages = array('', '');
824+
$messages[] = $emptyLine = $formatter->format(sprintf('<error>%s</error>', str_repeat(' ', $len)));
825+
$messages[] = $formatter->format(sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $strlen($title)))));
822826
foreach ($lines as $line) {
823-
$messages[] = $line.str_repeat(' ', $len - $strlen($line));
827+
$messages[] = $formatter->format(sprintf('<error> %s %s</error>', $line[0], str_repeat(' ', $len - $line[1])));
824828
}
829+
$messages[] = $emptyLine;
830+
$messages[] = '';
831+
$messages[] = '';
825832

826-
$messages[] = str_repeat(' ', $len);
827-
828-
$output->writeln("");
829-
$output->writeln("");
830-
foreach ($messages as $message) {
831-
$output->writeln('<error>'.OutputFormatter::escape($message).'</error>');
832-
}
833-
$output->writeln("");
834-
$output->writeln("");
833+
$output->writeln($messages, OutputInterface::OUTPUT_RAW);
835834

836835
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) {
837836
$output->writeln('<comment>Exception trace:</comment>');

src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
2020
try {
2121
throw new \Exception("First exception <p>this is html</p>");
2222
} catch (\Exception $e) {
23-
throw new \Exception("Second exception <comment>comment<comment>", 0, $e);
23+
throw new \Exception("Second exception <comment>comment</comment>", 0, $e);
2424
}
2525
} catch (\Exception $e) {
2626
throw new \Exception("Third exception <fg=blue;bg=red>comment</>", 0, $e);

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

22

3-
4-
[Exception]
5-
Third exception <fg=blue;bg=red>comment</>
6-
3+
4+
[Exception]
5+
Third exception comment
6+
77

88

99

1010

11-
12-
[Exception]
13-
Second exception <comment>comment<comment>
14-
11+
12+
[Exception]
13+
Second exception comment
14+
1515

1616

1717

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11

22

3-
 
4-
 [Exception] 
5-
 Third exception <fg=blue;bg=red>comment</> 
6-
 
3+
 
4+
 [Exception] 
5+
 Third exception comment 
6+
 
77

88

99

1010

11-
 
12-
 [Exception] 
13-
 Second exception <comment>comment<comment> 
14-
 
11+
 
12+
 [Exception] 
13+
 Second exception comment 
14+
 
1515

1616

1717

1818

1919
 
2020
 [Exception] 
21-
 First exception <p>this is html</p> 
21+
 First exception <p>this is html</p> 
2222
 
2323

2424

0 commit comments

Comments
 (0)