merged branch gigablah/terminal-dimensions-fix (PR #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.
This commit is contained in:
commit
24534ce00c
@ -784,7 +784,7 @@ class Application
|
|||||||
$len = $strlen($title);
|
$len = $strlen($title);
|
||||||
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
|
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
|
||||||
$lines = array();
|
$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) {
|
foreach (str_split($line, $width - 4) as $line) {
|
||||||
$lines[] = sprintf(' %s ', $line);
|
$lines[] = sprintf(' %s ', $line);
|
||||||
$len = max($strlen($line) + 4, $len);
|
$len = max($strlen($line) + 4, $len);
|
||||||
@ -848,19 +848,9 @@ class Application
|
|||||||
*/
|
*/
|
||||||
public function getTerminalWidth()
|
public function getTerminalWidth()
|
||||||
{
|
{
|
||||||
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
$dimensions = $this->getTerminalDimensions();
|
||||||
if ($ansicon = getenv('ANSICON')) {
|
|
||||||
return preg_replace('{^(\d+)x.*$}', '$1', $ansicon);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preg_match('{^(\d+)x\d+$}i', $this->getConsoleMode(), $matches)) {
|
return $dimensions[0];
|
||||||
return $matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) {
|
|
||||||
return $match[2];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -869,20 +859,42 @@ class Application
|
|||||||
* @return int|null
|
* @return int|null
|
||||||
*/
|
*/
|
||||||
public function getTerminalHeight()
|
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 (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
||||||
if ($ansicon = getenv('ANSICON')) {
|
// extract [w, H] from "wxh (WxH)"
|
||||||
return preg_replace('{^\d+x\d+ \(\d+x(\d+)\)$}', '$1', trim($ansicon));
|
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
|
||||||
|
return array((int) $matches[1], (int) $matches[2]);
|
||||||
}
|
}
|
||||||
|
// extract [w, h] from "wxh"
|
||||||
if (preg_match('{^\d+x(\d+)$}i', $this->getConsoleMode(), $matches)) {
|
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
|
||||||
return $matches[1];
|
return array((int) $matches[1], (int) $matches[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) {
|
if ($sttyString = $this->getSttyColumns()) {
|
||||||
return $match[1];
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -982,7 +994,7 @@ class Application
|
|||||||
fclose($pipes[2]);
|
fclose($pipes[2]);
|
||||||
proc_close($process);
|
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];
|
return $matches[2].'x'.$matches[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user