Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace SebastianBergmann\Environment; Chris@0: Chris@0: /** Chris@0: */ Chris@0: class Console Chris@0: { Chris@0: const STDIN = 0; Chris@0: const STDOUT = 1; Chris@0: const STDERR = 2; Chris@0: Chris@0: /** Chris@0: * Returns true if STDOUT supports colorization. Chris@0: * Chris@0: * This code has been copied and adapted from Chris@0: * Symfony\Component\Console\Output\OutputStream. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasColorSupport() Chris@0: { Chris@0: if (DIRECTORY_SEPARATOR == '\\') { Chris@0: return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM'); Chris@0: } Chris@0: Chris@0: if (!defined('STDOUT')) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return $this->isInteractive(STDOUT); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the number of columns of the terminal. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getNumberOfColumns() Chris@0: { Chris@0: if (DIRECTORY_SEPARATOR == '\\') { Chris@0: $columns = 80; Chris@0: Chris@0: if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { Chris@0: $columns = $matches[1]; Chris@0: } elseif (function_exists('proc_open')) { Chris@0: $process = proc_open( Chris@0: 'mode CON', Chris@0: array( Chris@0: 1 => array('pipe', 'w'), Chris@0: 2 => array('pipe', 'w') Chris@0: ), Chris@0: $pipes, Chris@0: null, Chris@0: null, Chris@0: array('suppress_errors' => true) Chris@0: ); Chris@0: Chris@0: if (is_resource($process)) { Chris@0: $info = stream_get_contents($pipes[1]); Chris@0: Chris@0: fclose($pipes[1]); Chris@0: fclose($pipes[2]); Chris@0: proc_close($process); Chris@0: Chris@0: if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { Chris@0: $columns = $matches[2]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $columns - 1; Chris@0: } Chris@0: Chris@0: if (!$this->isInteractive(self::STDIN)) { Chris@0: return 80; Chris@0: } Chris@0: Chris@0: if (function_exists('shell_exec') && preg_match('#\d+ (\d+)#', shell_exec('stty size'), $match) === 1) { Chris@0: if ((int) $match[1] > 0) { Chris@0: return (int) $match[1]; Chris@0: } Chris@0: } Chris@0: Chris@0: if (function_exists('shell_exec') && preg_match('#columns = (\d+);#', shell_exec('stty'), $match) === 1) { Chris@0: if ((int) $match[1] > 0) { Chris@0: return (int) $match[1]; Chris@0: } Chris@0: } Chris@0: Chris@0: return 80; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns if the file descriptor is an interactive terminal or not. Chris@0: * Chris@0: * @param int|resource $fileDescriptor Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isInteractive($fileDescriptor = self::STDOUT) Chris@0: { Chris@0: return function_exists('posix_isatty') && @posix_isatty($fileDescriptor); Chris@0: } Chris@0: }