Console.php (1904B)
1 <?php 2 /* 3 * This file is part of the Environment package. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 namespace SebastianBergmann\Environment; 12 13 /** 14 */ 15 class Console 16 { 17 const STDIN = 0; 18 const STDOUT = 1; 19 const STDERR = 2; 20 21 /** 22 * Returns true if STDOUT supports colorization. 23 * 24 * This code has been copied and adapted from 25 * Symfony\Component\Console\Output\OutputStream. 26 * 27 * @return bool 28 */ 29 public function hasColorSupport() 30 { 31 if (DIRECTORY_SEPARATOR == '\\') { 32 return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'); 33 } 34 35 if (!defined('STDOUT')) { 36 return false; 37 } 38 39 return $this->isInteractive(STDOUT); 40 } 41 42 /** 43 * Returns the number of columns of the terminal. 44 * 45 * @return int 46 */ 47 public function getNumberOfColumns() 48 { 49 // Windows terminals have a fixed size of 80 50 // but one column is used for the cursor. 51 if (DIRECTORY_SEPARATOR == '\\') { 52 return 79; 53 } 54 55 if (!$this->isInteractive(self::STDIN)) { 56 return 80; 57 } 58 59 if (preg_match('#\d+ (\d+)#', shell_exec('stty size'), $match) === 1) { 60 return (int) $match[1]; 61 } 62 63 if (preg_match('#columns = (\d+);#', shell_exec('stty'), $match) === 1) { 64 return (int) $match[1]; 65 } 66 67 return 80; 68 } 69 70 /** 71 * Returns if the file descriptor is an interactive terminal or not. 72 * 73 * @param int|resource $fileDescriptor 74 * 75 * @return bool 76 */ 77 public function isInteractive($fileDescriptor = self::STDOUT) 78 { 79 return function_exists('posix_isatty') && @posix_isatty($fileDescriptor); 80 } 81 }