Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console;
|
Chris@0
|
13
|
Chris@0
|
14 class Terminal
|
Chris@0
|
15 {
|
Chris@0
|
16 private static $width;
|
Chris@0
|
17 private static $height;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Gets the terminal width.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @return int
|
Chris@0
|
23 */
|
Chris@0
|
24 public function getWidth()
|
Chris@0
|
25 {
|
Chris@0
|
26 $width = getenv('COLUMNS');
|
Chris@0
|
27 if (false !== $width) {
|
Chris@0
|
28 return (int) trim($width);
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 if (null === self::$width) {
|
Chris@0
|
32 self::initDimensions();
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 return self::$width ?: 80;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Gets the terminal height.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return int
|
Chris@0
|
42 */
|
Chris@0
|
43 public function getHeight()
|
Chris@0
|
44 {
|
Chris@0
|
45 $height = getenv('LINES');
|
Chris@0
|
46 if (false !== $height) {
|
Chris@0
|
47 return (int) trim($height);
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 if (null === self::$height) {
|
Chris@0
|
51 self::initDimensions();
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 return self::$height ?: 50;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 private static function initDimensions()
|
Chris@0
|
58 {
|
Chris@17
|
59 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
60 if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
|
Chris@0
|
61 // extract [w, H] from "wxh (WxH)"
|
Chris@0
|
62 // or [w, h] from "wxh"
|
Chris@0
|
63 self::$width = (int) $matches[1];
|
Chris@0
|
64 self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
|
Chris@0
|
65 } elseif (null !== $dimensions = self::getConsoleMode()) {
|
Chris@0
|
66 // extract [w, h] from "wxh"
|
Chris@0
|
67 self::$width = (int) $dimensions[0];
|
Chris@0
|
68 self::$height = (int) $dimensions[1];
|
Chris@0
|
69 }
|
Chris@0
|
70 } elseif ($sttyString = self::getSttyColumns()) {
|
Chris@0
|
71 if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
|
Chris@0
|
72 // extract [w, h] from "rows h; columns w;"
|
Chris@0
|
73 self::$width = (int) $matches[2];
|
Chris@0
|
74 self::$height = (int) $matches[1];
|
Chris@0
|
75 } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
|
Chris@0
|
76 // extract [w, h] from "; h rows; w columns"
|
Chris@0
|
77 self::$width = (int) $matches[2];
|
Chris@0
|
78 self::$height = (int) $matches[1];
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Runs and parses mode CON if it's available, suppressing any error output.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return int[]|null An array composed of the width and the height or null if it could not be parsed
|
Chris@0
|
87 */
|
Chris@0
|
88 private static function getConsoleMode()
|
Chris@0
|
89 {
|
Chris@17
|
90 if (!\function_exists('proc_open')) {
|
Chris@0
|
91 return;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@17
|
94 $descriptorspec = [
|
Chris@17
|
95 1 => ['pipe', 'w'],
|
Chris@17
|
96 2 => ['pipe', 'w'],
|
Chris@17
|
97 ];
|
Chris@17
|
98 $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
|
Chris@17
|
99 if (\is_resource($process)) {
|
Chris@0
|
100 $info = stream_get_contents($pipes[1]);
|
Chris@0
|
101 fclose($pipes[1]);
|
Chris@0
|
102 fclose($pipes[2]);
|
Chris@0
|
103 proc_close($process);
|
Chris@0
|
104
|
Chris@0
|
105 if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
|
Chris@17
|
106 return [(int) $matches[2], (int) $matches[1]];
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Runs and parses stty -a if it's available, suppressing any error output.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @return string|null
|
Chris@0
|
115 */
|
Chris@0
|
116 private static function getSttyColumns()
|
Chris@0
|
117 {
|
Chris@17
|
118 if (!\function_exists('proc_open')) {
|
Chris@0
|
119 return;
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@17
|
122 $descriptorspec = [
|
Chris@17
|
123 1 => ['pipe', 'w'],
|
Chris@17
|
124 2 => ['pipe', 'w'],
|
Chris@17
|
125 ];
|
Chris@0
|
126
|
Chris@17
|
127 $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
|
Chris@17
|
128 if (\is_resource($process)) {
|
Chris@0
|
129 $info = stream_get_contents($pipes[1]);
|
Chris@0
|
130 fclose($pipes[1]);
|
Chris@0
|
131 fclose($pipes[2]);
|
Chris@0
|
132 proc_close($process);
|
Chris@0
|
133
|
Chris@0
|
134 return $info;
|
Chris@0
|
135 }
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|