Upgrade framework
This commit is contained in:
121
vendor/symfony/console/Terminal.php
vendored
121
vendor/symfony/console/Terminal.php
vendored
@@ -13,15 +13,14 @@ namespace Symfony\Component\Console;
|
||||
|
||||
class Terminal
|
||||
{
|
||||
private static $width;
|
||||
private static $height;
|
||||
private static ?int $width = null;
|
||||
private static ?int $height = null;
|
||||
private static ?bool $stty = null;
|
||||
|
||||
/**
|
||||
* Gets the terminal width.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getWidth()
|
||||
public function getWidth(): int
|
||||
{
|
||||
$width = getenv('COLUMNS');
|
||||
if (false !== $width) {
|
||||
@@ -37,10 +36,8 @@ class Terminal
|
||||
|
||||
/**
|
||||
* Gets the terminal height.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getHeight()
|
||||
public function getHeight(): int
|
||||
{
|
||||
$height = getenv('LINES');
|
||||
if (false !== $height) {
|
||||
@@ -54,20 +51,61 @@ class Terminal
|
||||
return self::$height ?: 50;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public static function hasSttyAvailable(): bool
|
||||
{
|
||||
if (null !== self::$stty) {
|
||||
return self::$stty;
|
||||
}
|
||||
|
||||
// skip check if exec function is disabled
|
||||
if (!\function_exists('exec')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
exec('stty 2>&1', $output, $exitcode);
|
||||
|
||||
return self::$stty = 0 === $exitcode;
|
||||
}
|
||||
|
||||
private static function initDimensions()
|
||||
{
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
|
||||
// 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 (!self::hasVt100Support() && self::hasSttyAvailable()) {
|
||||
// only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
|
||||
// testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
|
||||
self::initDimensionsUsingStty();
|
||||
} elseif (null !== $dimensions = self::getConsoleMode()) {
|
||||
// extract [w, h] from "wxh"
|
||||
self::$width = (int) $dimensions[0];
|
||||
self::$height = (int) $dimensions[1];
|
||||
}
|
||||
} elseif ($sttyString = self::getSttyColumns()) {
|
||||
} else {
|
||||
self::initDimensionsUsingStty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
|
||||
*/
|
||||
private static function hasVt100Support(): bool
|
||||
{
|
||||
return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes dimensions using the output of an stty columns line.
|
||||
*/
|
||||
private static function initDimensionsUsingStty()
|
||||
{
|
||||
if ($sttyString = self::getSttyColumns()) {
|
||||
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
|
||||
// extract [w, h] from "rows h; columns w;"
|
||||
self::$width = (int) $matches[2];
|
||||
@@ -85,53 +123,46 @@ class Terminal
|
||||
*
|
||||
* @return int[]|null An array composed of the width and the height or null if it could not be parsed
|
||||
*/
|
||||
private static function getConsoleMode()
|
||||
private static function getConsoleMode(): ?array
|
||||
{
|
||||
if (!function_exists('proc_open')) {
|
||||
return;
|
||||
$info = self::readFromProcess('mode CON');
|
||||
|
||||
if (null === $info || !preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$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)) {
|
||||
return array((int) $matches[2], (int) $matches[1]);
|
||||
}
|
||||
}
|
||||
return [(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()
|
||||
private static function getSttyColumns(): ?string
|
||||
{
|
||||
if (!function_exists('proc_open')) {
|
||||
return;
|
||||
return self::readFromProcess('stty -a | grep columns');
|
||||
}
|
||||
|
||||
private static function readFromProcess(string $command): ?string
|
||||
{
|
||||
if (!\function_exists('proc_open')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$descriptorspec = array(
|
||||
1 => array('pipe', 'w'),
|
||||
2 => array('pipe', 'w'),
|
||||
);
|
||||
$descriptorspec = [
|
||||
1 => ['pipe', 'w'],
|
||||
2 => ['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;
|
||||
$process = proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
|
||||
if (!\is_resource($process)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$info = stream_get_contents($pipes[1]);
|
||||
fclose($pipes[1]);
|
||||
fclose($pipes[2]);
|
||||
proc_close($process);
|
||||
|
||||
return $info;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user