Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console; Chris@0: Chris@0: class Terminal Chris@0: { Chris@0: private static $width; Chris@0: private static $height; Chris@0: Chris@0: /** Chris@0: * Gets the terminal width. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getWidth() Chris@0: { Chris@0: $width = getenv('COLUMNS'); Chris@0: if (false !== $width) { Chris@0: return (int) trim($width); Chris@0: } Chris@0: Chris@0: if (null === self::$width) { Chris@0: self::initDimensions(); Chris@0: } Chris@0: Chris@0: return self::$width ?: 80; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the terminal height. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getHeight() Chris@0: { Chris@0: $height = getenv('LINES'); Chris@0: if (false !== $height) { Chris@0: return (int) trim($height); Chris@0: } Chris@0: Chris@0: if (null === self::$height) { Chris@0: self::initDimensions(); Chris@0: } Chris@0: Chris@0: return self::$height ?: 50; Chris@0: } Chris@0: Chris@0: private static function initDimensions() Chris@0: { Chris@17: if ('\\' === \DIRECTORY_SEPARATOR) { Chris@0: if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) { Chris@0: // extract [w, H] from "wxh (WxH)" Chris@0: // or [w, h] from "wxh" Chris@0: self::$width = (int) $matches[1]; Chris@0: self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2]; Chris@0: } elseif (null !== $dimensions = self::getConsoleMode()) { Chris@0: // extract [w, h] from "wxh" Chris@0: self::$width = (int) $dimensions[0]; Chris@0: self::$height = (int) $dimensions[1]; Chris@0: } Chris@0: } elseif ($sttyString = self::getSttyColumns()) { Chris@0: if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { Chris@0: // extract [w, h] from "rows h; columns w;" Chris@0: self::$width = (int) $matches[2]; Chris@0: self::$height = (int) $matches[1]; Chris@0: } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { Chris@0: // extract [w, h] from "; h rows; w columns" Chris@0: self::$width = (int) $matches[2]; Chris@0: self::$height = (int) $matches[1]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs and parses mode CON if it's available, suppressing any error output. Chris@0: * Chris@0: * @return int[]|null An array composed of the width and the height or null if it could not be parsed Chris@0: */ Chris@0: private static function getConsoleMode() Chris@0: { Chris@17: if (!\function_exists('proc_open')) { Chris@0: return; Chris@0: } Chris@0: Chris@17: $descriptorspec = [ Chris@17: 1 => ['pipe', 'w'], Chris@17: 2 => ['pipe', 'w'], Chris@17: ]; Chris@17: $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); Chris@17: if (\is_resource($process)) { Chris@0: $info = stream_get_contents($pipes[1]); Chris@0: fclose($pipes[1]); Chris@0: fclose($pipes[2]); Chris@0: proc_close($process); Chris@0: Chris@0: if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { Chris@17: return [(int) $matches[2], (int) $matches[1]]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs and parses stty -a if it's available, suppressing any error output. Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: private static function getSttyColumns() Chris@0: { Chris@17: if (!\function_exists('proc_open')) { Chris@0: return; Chris@0: } Chris@0: Chris@17: $descriptorspec = [ Chris@17: 1 => ['pipe', 'w'], Chris@17: 2 => ['pipe', 'w'], Chris@17: ]; Chris@0: Chris@17: $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); Chris@17: if (\is_resource($process)) { Chris@0: $info = stream_get_contents($pipes[1]); Chris@0: fclose($pipes[1]); Chris@0: fclose($pipes[2]); Chris@0: proc_close($process); Chris@0: Chris@0: return $info; Chris@0: } Chris@0: } Chris@0: }