9
9
* file that was distributed with this source code.
10
10
*/
11
11
12
- namespace Symfony \Component \Console \ Terminal ;
12
+ namespace Symfony \Component \Console ;
13
13
14
- class TerminalDimensionsProvider
14
+ class Terminal
15
15
{
16
- /**
17
- * @var int[]
18
- */
19
- private $ terminalDimensions = array ();
16
+ private $ width ;
17
+ private $ height ;
20
18
21
19
/**
22
- * Tries to figure out the terminal dimensions based on the current environment .
20
+ * Tries to figure out the terminal width in which this application runs .
23
21
*
24
- * @return int[] Array containing width and height
22
+ * @return int|null
25
23
*/
26
- public function getTerminalDimensions ()
24
+ public function getWidth ()
27
25
{
28
- if ($ this ->terminalDimensions ) {
29
- return $ this ->terminalDimensions ;
30
- }
31
-
32
- if ($ this ->isWindowsEnvironment ()) {
33
- // extract [w, H] from "wxh (WxH)"
34
- if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
35
- return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
36
- }
37
- // extract [w, h] from "wxh"
38
- if (preg_match ('/^(\d+)x(\d+)$/ ' , $ this ->getConsoleMode (), $ matches )) {
39
- return array ((int ) $ matches [1 ], (int ) $ matches [2 ]);
40
- }
26
+ if (null === $ this ->width ) {
27
+ $ this ->initDimensions ();
41
28
}
42
29
43
- if ($ sttyString = $ this ->getSttyColumns ()) {
44
- // extract [w, h] from "rows h; columns w;"
45
- if (preg_match ('/rows.(\d+);.columns.(\d+);/i ' , $ sttyString , $ matches )) {
46
- return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
47
- }
48
- // extract [w, h] from "; h rows; w columns"
49
- if (preg_match ('/;.(\d+).rows;.(\d+).columns/i ' , $ sttyString , $ matches )) {
50
- return array ((int ) $ matches [2 ], (int ) $ matches [1 ]);
51
- }
52
- }
53
-
54
- return array (null , null );
30
+ return $ this ->width ;
55
31
}
56
32
57
33
/**
58
- * Tries to figure out the terminal width in which this application runs .
34
+ * Sets the terminal width.
59
35
*
60
- * @return int|null
36
+ * @param int
61
37
*/
62
- public function getTerminalWidth ( )
38
+ public function setWidth ( $ width )
63
39
{
64
- return $ this ->getTerminalDimensions ()[ 0 ] ;
40
+ $ this ->width = $ width ;
65
41
}
66
42
67
43
/**
68
44
* Tries to figure out the terminal height in which this application runs.
69
45
*
70
46
* @return int|null
71
47
*/
72
- public function getTerminalHeight ()
48
+ public function getHeight ()
73
49
{
74
- return $ this ->getTerminalDimensions ()[1 ];
50
+ if (null === $ this ->height ) {
51
+ $ this ->initDimensions ();
52
+ }
53
+
54
+ return $ this ->height ;
75
55
}
76
56
77
57
/**
78
- * Sets terminal dimensions.
79
- *
80
- * Can be useful to force terminal dimensions for functional tests.
58
+ * Sets the terminal height.
81
59
*
82
- * @param int $width
83
- * @param int $height
60
+ * @param int
84
61
*/
85
- public function setTerminalDimensions ( $ width , $ height )
62
+ public function setHeight ( $ height )
86
63
{
87
- $ this ->terminalDimensions = array ($ width , $ height );
64
+ $ this ->height = $ height ;
65
+ }
66
+
67
+ private function initDimensions ()
68
+ {
69
+ if (null !== $ this ->width && null !== $ this ->height ) {
70
+ return ;
71
+ }
72
+
73
+ $ width = $ height = null ;
74
+ if ($ this ->isWindowsEnvironment ()) {
75
+ if (preg_match ('/^(\d+)x\d+ \(\d+x(\d+)\)$/ ' , trim (getenv ('ANSICON ' )), $ matches )) {
76
+ // extract [w, H] from "wxh (WxH)"
77
+ $ width = (int ) $ matches [1 ];
78
+ $ height = (int ) $ matches [2 ];
79
+ } elseif (null != $ dimensions = $ this ->getConsoleMode ()) {
80
+ // extract [w, h] from "wxh"
81
+ $ width = $ dimensions [0 ];
82
+ $ height = $ dimensions [1 ];
83
+ }
84
+ } elseif ($ sttyString = $ this ->getSttyColumns ()) {
85
+ if (preg_match ('/rows.(\d+);.columns.(\d+);/i ' , $ sttyString , $ matches )) {
86
+ // extract [w, h] from "rows h; columns w;"
87
+ $ width = (int ) $ matches [1 ];
88
+ $ height = (int ) $ matches [2 ];
89
+ } elseif (preg_match ('/;.(\d+).rows;.(\d+).columns/i ' , $ sttyString , $ matches )) {
90
+ // extract [w, h] from "; h rows; w columns"
91
+ $ width = (int ) $ matches [2 ];
92
+ $ heighth = (int ) $ matches [1 ];
93
+ }
94
+ }
95
+
96
+ if (null === $ this ->width ) {
97
+ $ this ->width = $ width ;
98
+ }
99
+
100
+ if (null === $ this ->height ) {
101
+ $ this ->height = $ height ;
102
+ }
88
103
}
89
104
90
105
/**
91
106
* Runs and parses mode CON if it's available, suppressing any error output.
92
107
*
93
- * @return string < width>x< height> or null if it could not be parsed
108
+ * @return array|null An array composed of the width and the height or null if it could not be parsed
94
109
*/
95
110
private function getConsoleMode ()
96
111
{
@@ -110,7 +125,7 @@ private function getConsoleMode()
110
125
proc_close ($ process );
111
126
112
127
if (preg_match ('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/ ' , $ info , $ matches )) {
113
- return $ matches [2 ]. ' x ' . $ matches [1 ];
128
+ return array (( int ) $ matches [2 ], ( int ) $ matches [1 ]) ;
114
129
}
115
130
}
116
131
}
0 commit comments