Attribute
1 year ago
CI
1 year ago
Command
1 year ago
CommandLoader
2 years ago
Completion
1 year ago
DependencyInjection
1 year ago
Descriptor
1 year ago
Event
1 year ago
EventListener
2 years ago
Exception
2 years ago
Formatter
1 year ago
Helper
1 year ago
Input
1 year ago
Logger
1 year ago
Output
1 year ago
Question
1 year ago
Resources
1 year ago
SignalRegistry
1 year ago
Style
1 year ago
Tester
1 year ago
Application.php
1 year ago
Color.php
1 year ago
ConsoleEvents.php
2 years ago
Cursor.php
2 years ago
LICENSE
2 years ago
README.md
2 years ago
SingleCommandApplication.php
1 year ago
Terminal.php
1 year ago
Terminal.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console; |
| 12 | |
| 13 | class Terminal |
| 14 | { |
| 15 | private static $width; |
| 16 | private static $height; |
| 17 | private static $stty; |
| 18 | /** |
| 19 | * Gets the terminal width. |
| 20 | * |
| 21 | * @return int |
| 22 | */ |
| 23 | public function getWidth() |
| 24 | { |
| 25 | $width = getenv('COLUMNS'); |
| 26 | if (\false !== $width) { |
| 27 | return (int) trim($width); |
| 28 | } |
| 29 | if (null === self::$width) { |
| 30 | self::initDimensions(); |
| 31 | } |
| 32 | return self::$width ?: 80; |
| 33 | } |
| 34 | /** |
| 35 | * Gets the terminal height. |
| 36 | * |
| 37 | * @return int |
| 38 | */ |
| 39 | public function getHeight() |
| 40 | { |
| 41 | $height = getenv('LINES'); |
| 42 | if (\false !== $height) { |
| 43 | return (int) trim($height); |
| 44 | } |
| 45 | if (null === self::$height) { |
| 46 | self::initDimensions(); |
| 47 | } |
| 48 | return self::$height ?: 50; |
| 49 | } |
| 50 | /** |
| 51 | * @internal |
| 52 | */ |
| 53 | public static function hasSttyAvailable() : bool |
| 54 | { |
| 55 | if (null !== self::$stty) { |
| 56 | return self::$stty; |
| 57 | } |
| 58 | // skip check if shell_exec function is disabled |
| 59 | if (!\function_exists('shell_exec')) { |
| 60 | return \false; |
| 61 | } |
| 62 | return self::$stty = (bool) shell_exec('stty 2> ' . ('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null')); |
| 63 | } |
| 64 | private static function initDimensions() |
| 65 | { |
| 66 | if ('\\' === \DIRECTORY_SEPARATOR) { |
| 67 | $ansicon = getenv('ANSICON'); |
| 68 | if (\false !== $ansicon && preg_match('/^(\\d+)x(\\d+)(?: \\((\\d+)x(\\d+)\\))?$/', trim($ansicon), $matches)) { |
| 69 | // extract [w, H] from "wxh (WxH)" |
| 70 | // or [w, h] from "wxh" |
| 71 | self::$width = (int) $matches[1]; |
| 72 | self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2]; |
| 73 | } elseif (!self::hasVt100Support() && self::hasSttyAvailable()) { |
| 74 | // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash) |
| 75 | // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT |
| 76 | self::initDimensionsUsingStty(); |
| 77 | } elseif (null !== ($dimensions = self::getConsoleMode())) { |
| 78 | // extract [w, h] from "wxh" |
| 79 | self::$width = (int) $dimensions[0]; |
| 80 | self::$height = (int) $dimensions[1]; |
| 81 | } |
| 82 | } else { |
| 83 | self::initDimensionsUsingStty(); |
| 84 | } |
| 85 | } |
| 86 | /** |
| 87 | * Returns whether STDOUT has vt100 support (some Windows 10+ configurations). |
| 88 | */ |
| 89 | private static function hasVt100Support() : bool |
| 90 | { |
| 91 | return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w')); |
| 92 | } |
| 93 | /** |
| 94 | * Initializes dimensions using the output of an stty columns line. |
| 95 | */ |
| 96 | private static function initDimensionsUsingStty() |
| 97 | { |
| 98 | if ($sttyString = self::getSttyColumns()) { |
| 99 | if (preg_match('/rows.(\\d+);.columns.(\\d+);/i', $sttyString, $matches)) { |
| 100 | // extract [w, h] from "rows h; columns w;" |
| 101 | self::$width = (int) $matches[2]; |
| 102 | self::$height = (int) $matches[1]; |
| 103 | } elseif (preg_match('/;.(\\d+).rows;.(\\d+).columns/i', $sttyString, $matches)) { |
| 104 | // extract [w, h] from "; h rows; w columns" |
| 105 | self::$width = (int) $matches[2]; |
| 106 | self::$height = (int) $matches[1]; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | /** |
| 111 | * Runs and parses mode CON if it's available, suppressing any error output. |
| 112 | * |
| 113 | * @return int[]|null An array composed of the width and the height or null if it could not be parsed |
| 114 | */ |
| 115 | private static function getConsoleMode() : ?array |
| 116 | { |
| 117 | $info = self::readFromProcess('mode CON'); |
| 118 | if (null === $info || !preg_match('/--------+\\r?\\n.+?(\\d+)\\r?\\n.+?(\\d+)\\r?\\n/', $info, $matches)) { |
| 119 | return null; |
| 120 | } |
| 121 | return [(int) $matches[2], (int) $matches[1]]; |
| 122 | } |
| 123 | /** |
| 124 | * Runs and parses stty -a if it's available, suppressing any error output. |
| 125 | */ |
| 126 | private static function getSttyColumns() : ?string |
| 127 | { |
| 128 | return self::readFromProcess('stty -a | grep columns'); |
| 129 | } |
| 130 | private static function readFromProcess(string $command) : ?string |
| 131 | { |
| 132 | if (!\function_exists('proc_open')) { |
| 133 | return null; |
| 134 | } |
| 135 | $descriptorspec = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; |
| 136 | $cp = \function_exists('sapi_windows_cp_set') ? sapi_windows_cp_get() : 0; |
| 137 | if (!($process = @proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => \true]))) { |
| 138 | return null; |
| 139 | } |
| 140 | $info = stream_get_contents($pipes[1]); |
| 141 | fclose($pipes[1]); |
| 142 | fclose($pipes[2]); |
| 143 | proc_close($process); |
| 144 | if ($cp) { |
| 145 | sapi_windows_cp_set($cp); |
| 146 | } |
| 147 | return $info; |
| 148 | } |
| 149 | } |
| 150 |