Skip to content

Commit ba75e66

Browse files
committed
avoid output when tput is missing. fixes #5
1 parent 1d6f0bf commit ba75e66

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/TableFormatter.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ class TableFormatter
2929
public function __construct(Colors $colors = null)
3030
{
3131
// 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();
4133
if ($width) {
4234
$this->max = $width - 1;
4335
}
@@ -92,6 +84,27 @@ public function setMaxWidth($max)
9284
$this->max = $max;
9385
}
9486

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+
95108
/**
96109
* Takes an array with dynamic column width and calculates the correct width
97110
*

0 commit comments

Comments
 (0)