Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Screen.php
Go to the documentation of this file.
1<?php
3
10class Screen {
11
12 private static $width;
13
14 private static $height;
15
16 private static $stty;
17
23 public static function getWidth() {
24 $width = \getenv('COLUMNS');
25 if (false !== $width) {
26 return (int) \trim($width);
27 }
28
29 if (null === self::$width) {
31 }
32
33 return self::$width ?? 80;
34 }
35
41 public static function getHeight() {
42 $height = \getenv('LINES');
43 if (false !== $height) {
44 return (int) trim($height);
45 }
46
47 if (null === self::$height) {
49 }
50
51 return self::$height ?? 50;
52 }
53
60 public static function hasSttyAvailable() {
61 if (null !== self::$stty) {
62 return self::$stty;
63 }
64
65 // skip check if exec function is disabled
66 if (! \function_exists('exec')) {
67 return false;
68 }
69
70 \exec('stty 2>&1', $output, $exitcode);
71
72 return self::$stty = 0 === $exitcode;
73 }
74
75 private static function initDimensions() {
76 if ('\\' === \DIRECTORY_SEPARATOR) {
77 if (\preg_match('/^(\d+)x(\d+)(?: \‍((\d+)x(\d+)\‍))?$/', \trim(\getenv('ANSICON')), $matches)) {
78 // extract [w, H] from "wxh (WxH)"
79 // or [w, h] from "wxh"
80 self::$width = (int) $matches[1];
81 self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
82 } elseif (! self::hasVt100Support() && self::hasSttyAvailable()) {
83 // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
84 // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
86 } elseif (null !== $dimensions = self::getConsoleMode()) {
87 // extract [w, h] from "wxh"
88 self::$width = (int) $dimensions[0];
89 self::$height = (int) $dimensions[1];
90 }
91 } else {
93 }
94 }
95
99 private static function hasVt100Support(): bool {
100 return \function_exists('sapi_windows_vt100_support') && \sapi_windows_vt100_support(\fopen('php://stdout', 'w'));
101 }
102
106 private static function initDimensionsUsingStty() {
107 if ($sttyString = self::getSttyColumns()) {
108 if (\preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
109 // extract [w, h] from "rows h; columns w;"
110 self::$width = (int) $matches[2];
111 self::$height = (int) $matches[1];
112 } elseif (\preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
113 // extract [w, h] from "; h rows; w columns"
114 self::$width = (int) $matches[2];
115 self::$height = (int) $matches[1];
116 }
117 }
118 }
119
125 private static function getConsoleMode(): ?array {
126 $info = self::readFromProcess('mode CON');
127
128 if (null === $info || ! preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
129 return null;
130 }
131
132 return [
133 (int) $matches[2],
134 (int) $matches[1]
135 ];
136 }
137
141 private static function getSttyColumns(): ?string {
142 return self::readFromProcess('stty -a | grep columns');
143 }
144
145 private static function readFromProcess(string $command): ?string {
146 if (! \function_exists('proc_open')) {
147 return null;
148 }
149
150 $descriptorspec = [
151 1 => [
152 'pipe',
153 'w'
154 ],
155 2 => [
156 'pipe',
157 'w'
158 ]
159 ];
160
161 $process = \proc_open($command, $descriptorspec, $pipes, null, null, [
162 'suppress_errors' => true
163 ]);
164 if (! \is_resource($process)) {
165 return null;
166 }
167
168 $info = \stream_get_contents($pipes[1]);
169 \fclose($pipes[1]);
170 \fclose($pipes[2]);
171 \proc_close($process);
172
173 return $info;
174 }
175}
static getWidth()
Gets the terminal width.
Definition Screen.php:23
static getSttyColumns()
Runs and parses stty -a if it's available, suppressing any error output.
Definition Screen.php:141
static hasVt100Support()
Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
Definition Screen.php:99
static readFromProcess(string $command)
Definition Screen.php:145
static initDimensionsUsingStty()
Initializes dimensions using the output of an stty columns line.
Definition Screen.php:106
static getHeight()
Gets the terminal height.
Definition Screen.php:41
static getConsoleMode()
Runs and parses mode CON if it's available, suppressing any error output.
Definition Screen.php:125