bug #22188 [Console] Revised exception rendering (ro0NL)

This PR was squashed before being merged into the 2.7 branch (closes #22188).

Discussion
----------

[Console] Revised exception rendering

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Continuation of #22142, as escaping can break too i just realized.

Before

![image](https://cloud.githubusercontent.com/assets/1047696/24376962/fc85a15c-133d-11e7-90fe-a8c754cbf592.png)

After

![image](https://cloud.githubusercontent.com/assets/1047696/24377289/340a8f2e-133f-11e7-83b3-54e1ea6f46bd.png)

cc @chalasr

Commits
-------

17f1f079b2 [Console] Revised exception rendering
This commit is contained in:
Fabien Potencier 2017-03-29 07:27:06 +02:00
commit 11a06ccced
3 changed files with 31 additions and 7 deletions

View File

@ -650,12 +650,11 @@ class Application
if (defined('HHVM_VERSION') && $width > 1 << 31) {
$width = 1 << 31;
}
$formatter = $output->getFormatter();
$lines = array();
foreach (preg_split('/\r?\n/', OutputFormatter::escape($e->getMessage())) as $line) {
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
// pre-format lines to get the right string length
$lineLength = $this->stringWidth(preg_replace('/\[[^m]*m/', '', $formatter->format($line))) + 4;
$lineLength = $this->stringWidth($line) + 4;
$lines[] = array($line, $lineLength);
$len = max($lineLength, $len);
@ -663,15 +662,15 @@ class Application
}
$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 - $this->stringWidth($title)))));
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
foreach ($lines as $line) {
$messages[] = $formatter->format(sprintf('<error> %s %s</error>', $line[0], str_repeat(' ', $len - $line[1])));
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
}
$messages[] = $emptyLine;
$messages[] = '';
$output->writeln($messages, OutputInterface::OUTPUT_RAW);
$output->writeln($messages);
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$output->writeln('<comment>Exception trace:</comment>');

View File

@ -591,6 +591,22 @@ class ApplicationTest extends TestCase
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
}
public function testRenderExceptionEscapesLines()
{
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
$application->setAutoExit(false);
$application->expects($this->any())
->method('getTerminalWidth')
->will($this->returnValue(22));
$application->register('foo')->setCode(function () {
throw new \Exception('dont break here <info>!</info>');
});
$tester = new ApplicationTester($application);
$tester->run(array('command' => 'foo'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
}
public function testRun()
{
$application = new Application();

View File

@ -0,0 +1,9 @@
[Exception]
dont break here <
info>!</info>
foo