File tree Expand file tree Collapse file tree 1 file changed +22
-9
lines changed Expand file tree Collapse file tree 1 file changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -29,15 +29,7 @@ class TableFormatter
29
29
public function __construct (Colors $ colors = null )
30
30
{
31
31
// try to get terminal width
32
- $ width = 0 ;
33
- if (isset ($ _SERVER ['COLUMNS ' ])) {
34
- // from environment
35
- $ width = (int )$ _SERVER ['COLUMNS ' ];
36
- }
37
- if (!$ width ) {
38
- // via tput command
39
- $ width = @exec ('tput cols ' );
40
- }
32
+ $ width = $ this ->getTerminalWidth ();
41
33
if ($ width ) {
42
34
$ this ->max = $ width - 1 ;
43
35
}
@@ -92,6 +84,27 @@ public function setMaxWidth($max)
92
84
$ this ->max = $ max ;
93
85
}
94
86
87
+ /**
88
+ * Tries to figure out the width of the terminal
89
+ *
90
+ * @return int terminal width, 0 if unknown
91
+ */
92
+ protected function getTerminalWidth ()
93
+ {
94
+ // from environment
95
+ if (isset ($ _SERVER ['COLUMNS ' ])) return (int )$ _SERVER ['COLUMNS ' ];
96
+
97
+ // via tput
98
+ $ process = proc_open ('tput cols ' , array (
99
+ 1 => array ('pipe ' , 'w ' ),
100
+ 2 => array ('pipe ' , 'w ' ),
101
+ ), $ pipes );
102
+ $ width = (int )stream_get_contents ($ pipes [1 ]);
103
+ proc_close ($ process );
104
+
105
+ return $ width ;
106
+ }
107
+
95
108
/**
96
109
* Takes an array with dynamic column width and calculates the correct width
97
110
*
You can’t perform that action at this time.
0 commit comments