Skip to content

Commit

Permalink
[2.1] [Console] Added getTerminalDimensions() with fix for osx/freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
gigablah committed Jan 5, 2013
1 parent d50df09 commit 8f21f89
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public function renderException($e, $output)
$len = $strlen($title);
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
$lines = array();
foreach (preg_split("{\r?\n}", $e->getMessage()) as $line) {
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);
Expand Down Expand Up @@ -848,19 +848,9 @@ public function renderException($e, $output)
*/
public function getTerminalWidth()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if ($ansicon = getenv('ANSICON')) {
return preg_replace('{^(\d+)x.*$}', '$1', $ansicon);
}

if (preg_match('{^(\d+)x\d+$}i', $this->getConsoleMode(), $matches)) {
return $matches[1];
}
}
$dimensions = $this->getTerminalDimensions();

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

/**
Expand All @@ -869,20 +859,42 @@ public function getTerminalWidth()
* @return int|null
*/
public function getTerminalHeight()
{
$dimensions = $this->getTerminalDimensions();

return $dimensions[1];
}

/**
* Tries to figure out the terminal dimensions based on the current environment
*
* @return array Array containing width and height
*/
protected function getTerminalDimensions()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if ($ansicon = getenv('ANSICON')) {
return preg_replace('{^\d+x\d+ \(\d+x(\d+)\)$}', '$1', trim($ansicon));
// extract [w, H] from "wxh (WxH)"
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
return array((int) $matches[1], (int) $matches[2]);
}

if (preg_match('{^\d+x(\d+)$}i', $this->getConsoleMode(), $matches)) {
return $matches[1];
// extract [w, h] from "wxh"
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
return array((int) $matches[1], (int) $matches[2]);
}
}

if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) {
return $match[1];
if ($sttyString = $this->getSttyColumns()) {
// extract [w, h] from "rows h; columns w;"
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
return array((int) $matches[2], (int) $matches[1]);
}
// extract [w, h] from "; h rows; w columns"
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
return array((int) $matches[2], (int) $matches[1]);
}
}

return array(null, null);
}

/**
Expand Down Expand Up @@ -982,7 +994,7 @@ private function getConsoleMode()
fclose($pipes[2]);
proc_close($process);

if (preg_match('{--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n}', $info, $matches)) {
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
return $matches[2].'x'.$matches[1];
}
}
Expand Down

0 comments on commit 8f21f89

Please sign in to comment.