8000 merged branch gigablah/terminal-dimensions-fix (PR #6581) · Ninir/symfony@24534ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 24534ce

Browse files
committed
merged branch gigablah/terminal-dimensions-fix (PR symfony#6581)
This PR was merged into the 2.1 branch. Commits ------- 8f21f89 [2.1] [Console] Added getTerminalDimensions() with fix for osx/freebsd Discussion ---------- [2.1] [Console] Added getTerminalDimensions() with fix for osx/freebsd Bug fix: yes Feature addition: no Backwards compatibility break: no Fixes the following tickets: - Todo: - License of the code: MIT Documentation PR: - For non-windows systems, the Console component makes use of `stty -a` to grab the dimensions of the terminal when formatting exception output. The regex pattern assumes a string like the following: `speed 38400 baud; rows 25; columns 80; line = 0;` However on OSX (and FreeBSD) the pattern is different: `speed 38400 baud; 25 rows; 80 columns;` This patch adds a fix to match the correct pattern on darwin/freebsd systems, and consolidates the code in `getTerminalWidth()` and `getTerminalHeight()` into a general `getTerminalDimensions()` function. I've also taken the liberty of changing the curly brace regex delimiters to forward slashes for consistency with the rest of the codebase.
2 parents d50df09 + 8f21f89 commit 24534ce

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ public function renderException($e, $output)
784784
$len = $strlen($title);
785785
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
786786
$lines = array();
787-
foreach (preg_split("{\r?\n}", $e->getMessage()) as $line) {
787+
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
788788
foreach (str_split($line, $width - 4) as $line) {
789789
$lines[] = sprintf(' %s ', $line);
790790
$len = max($strlen($line) + 4, $len);
@@ -848,19 +848,9 @@ public function renderException($e, $output)
848848
*/
849849
public function getTerminalWidth()
850850
{
851-
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
852-
if ($ansicon = getenv('ANSICON')) {
853-
return preg_replace('{^(\d+)x.*$}', '$1', $ansicon);
854-
}
855-
856-
if (preg_match('{^(\d+)x\d+$}i', $this->getConsoleMode(), $matches)) {
857-
return $matches[1];
858-
}
859-
}
851+
$dimensions = $this->getTerminalDimensions();
860852

861-
if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) {
862-
return $match[2];
863-
}
853+
return $dimensions[0];
864854
}
865855

866856
/**
@@ -869,20 +859,42 @@ public function getTerminalWidth()
869859
* @return int|null
870860
*/
871861
public function getTerminalHeight()
862+
{
863+
$dimensions = $this->getTerminalDimensions();
864+
865+
return $dimensions[1];
866+
}
867+
868+
/**
869+
* Tries to figure out the terminal dimensions based on the current environment
870+
*
871+
* @return array Array containing width and height
872+
*/
873+
protected function getTerminalDimensions()
872874
{
873875< 10000 /td>
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
874-
if ($ansicon = getenv('ANSICON')) {
875-
return preg_replace('{^\d+x\d+ \(\d+x(\d+)\)$}', '$1', trim($ansicon));
876+
// extract [w, H] from "wxh (WxH)"
877+
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
878+
return array((int) $matches[1], (int) $matches[2]);
876879
}
877-
878-
if (preg_match('{^\d+x(\d+)$}i', $this->getConsoleMode(), $matches)) {
879-
return $matches[1];
880+
// extract [w, h] from "wxh"
881+
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
882+
return array((int) $matches[1], (int) $matches[2]);
880883
}
881884
}
882885

883-
if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) {
884-
return $match[1];
886+
if ($sttyString = $this->getSttyColumns()) {
887+
// extract [w, h] from "rows h; columns w;"
888+
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
889+
return array((int) $matches[2], (int) $matches[1]);
890+
}
891+
// extract [w, h] from "; h rows; w columns"
892+
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
893+
return array((int) $matches[2], (int) $matches[1]);
894+
}
885895
}
896+
897+
return array(null, null);
886898
}
887899

888900
/**
@@ -982,7 +994,7 @@ private function getConsoleMode()
982994
fclose($pipes[2]);
983995
proc_close($process);
984996

985-
if (preg_match('{--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n}', $info, $matches)) {
997+
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
986998
return $matches[2].'x'.$matches[1];
987999
}
9881000
}

0 commit comments

Comments
 (0)
0