8000 merged branch Seldaek/console_ex_20 (PR #3802) · symfony/symfony@f2398f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2398f6

Browse files
committed
merged branch Seldaek/console_ex_20 (PR #3802)
Commits ------- 595cc11 [Console] Wrap exception messages to the terminal width to avoid ugly output 97f7b29 [Console] Avoid outputing \r's in exception messages Discussion ---------- [Console] Exception rendering fixes This fixes two things: - `\r`'s in exception messages were output (in case of `\r\n` newlines), creating really weird results on windows. - long exception messages were wrapping and then the "red" block was completely messed up, with half black/half red lines, now it's wrapped before output if the terminal width can be detected. If you don't care about merging this for 2.0, you can also merge the `console_ex` branch which applies on master. Due to moving tests and renaming of some normalize stuff in the tests, the two test patches are kind of different. RFC: I am really not sure where to put those getTerminalWidth/Height methods. I guess this is not the best place.
2 parents c140386 + 595cc11 commit f2398f6

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,16 @@ public function renderException($e, $output)
733733
do {
734734
$title = sprintf(' [%s] ', get_class($e));
735735
$len = $strlen($title);
736+
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
736737
$lines = array();
737-
foreach (explode("\n", $e->getMessage()) as $line) {
738-
$lines[] = sprintf(' %s ', $line);
739-
$len = max($strlen($line) + 4, $len);
738+
foreach (preg_split("{\r?\n}", $e->getMessage()) as $line) {
739+
foreach (str_split($line, $width - 4) as $line) {
740+
$lines[] = sprintf(' %s ', $line);
741+
$len = max($strlen($line) + 4, $len);
742+
}
740743
}
741744

742-
$messages = array(str_repeat(' ', $len), $title.str_repeat(' ', $len - $strlen($title)));
745+
$messages = array(str_repeat(' ', $len), $title.str_repeat(' ', max(0, $len - $strlen($title))));
743746

744747
foreach ($lines as $line) {
745748
$messages[] = $line.str_repeat(' ', $len - $strlen($line));
@@ -786,6 +789,28 @@ public function renderException($e, $output)
786789
}
787790
}
788791

792+
protected function getTerminalWidth()
793+
{
794+
if (defined('PHP_WINDOWS_VERSION_BUILD') && $ansicon = getenv('ANSICON')) {
795+
return preg_replace('{^(\d+)x.*$}', '$1', $ansicon);
796+
}
797+
798+
if (preg_match("{rows.(\d+);.columns.(\d+);}i", exec('stty -a | grep columns'), $match)) {
799+
return $match[1];
800+
}
801+
}
802+
803+
protected function getTerminalHeight()
804+
{
805+
if (defined('PHP_WINDOWS_VERSION_BUILD') && $ansicon = getenv('ANSICON')) {
806+
return preg_replace('{^\d+x\d+ \(\d+x(\d+)\)$}', '$1', trim($ansicon));
807+
}
808+
809+
if (preg_match("{rows.(\d+);.columns.(\d+);}i", exec('stty -a | grep columns'), $match)) {
810+
return $match[2];
811+
}
812+
}
813+
789814
/**
790815
* Gets the name of the command based on input.
791816
*

tests/Symfony/Tests/Component/Console/ApplicationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ public function testRenderException()
255255
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
256256
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $this->normalize($tester->getDisplay()), '->renderException() renders a pretty exceptions with previous exceptions');
257257

258+
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
259+
$application->setAutoExit(false);
260+
$application->expects($this->any())
261+
->method('getTerminalWidth')
262+
->will($this->returnValue(32));
263+
$tester = new ApplicationTester($application);
264+
265+
$tester->run(array('command' => 'foo'), array('decorated' => false));
266+
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $this->normalize($tester->getDisplay()), '->renderException() wraps messages when they are bigger than the terminal');
258267
}
259268

260269
public function testRun()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
4+
[InvalidArgumentException]
5+
Command "foo" is not define
6+
d.
7+
8+
9+

0 commit comments

Comments
 (0)
0