3 * This file is part of the Environment package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Environment;
22 * Returns true if STDOUT supports colorization.
24 * This code has been copied and adapted from
25 * Symfony\Component\Console\Output\OutputStream.
29 public function hasColorSupport()
31 if (DIRECTORY_SEPARATOR == '\\') {
32 return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
35 if (!defined('STDOUT')) {
39 return $this->isInteractive(STDOUT);
43 * Returns the number of columns of the terminal.
47 public function getNumberOfColumns()
49 if (DIRECTORY_SEPARATOR == '\\') {
52 if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
53 $columns = $matches[1];
54 } elseif (function_exists('proc_open')) {
58 1 => array('pipe', 'w'),
59 2 => array('pipe', 'w')
64 array('suppress_errors' => true)
67 if (is_resource($process)) {
68 $info = stream_get_contents($pipes[1]);
74 if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
75 $columns = $matches[2];
83 if (!$this->isInteractive(self::STDIN)) {
87 if (function_exists('shell_exec') && preg_match('#\d+ (\d+)#', shell_exec('stty size'), $match) === 1) {
88 if ((int) $match[1] > 0) {
89 return (int) $match[1];
93 if (function_exists('shell_exec') && preg_match('#columns = (\d+);#', shell_exec('stty'), $match) === 1) {
94 if ((int) $match[1] > 0) {
95 return (int) $match[1];
103 * Returns if the file descriptor is an interactive terminal or not.
105 * @param int|resource $fileDescriptor
109 public function isInteractive($fileDescriptor = self::STDOUT)
111 return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);