This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Terminal.php

136 lines
3.9 KiB
PHP
Raw Normal View History

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