Console.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/environment.
  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. namespace SebastianBergmann\Environment;
  11. use const DIRECTORY_SEPARATOR;
  12. use const STDIN;
  13. use const STDOUT;
  14. use function defined;
  15. use function fclose;
  16. use function fstat;
  17. use function function_exists;
  18. use function getenv;
  19. use function is_resource;
  20. use function is_string;
  21. use function posix_isatty;
  22. use function preg_match;
  23. use function proc_close;
  24. use function proc_open;
  25. use function sapi_windows_vt100_support;
  26. use function shell_exec;
  27. use function stream_get_contents;
  28. use function stream_isatty;
  29. use function trim;
  30. final class Console
  31. {
  32. /**
  33. * @var int
  34. */
  35. public const STDIN = 0;
  36. /**
  37. * @var int
  38. */
  39. public const STDOUT = 1;
  40. /**
  41. * @var int
  42. */
  43. public const STDERR = 2;
  44. /**
  45. * Returns true if STDOUT supports colorization.
  46. *
  47. * This code has been copied and adapted from
  48. * Symfony\Component\Console\Output\StreamOutput.
  49. */
  50. public function hasColorSupport(): bool
  51. {
  52. if ('Hyper' === getenv('TERM_PROGRAM')) {
  53. return true;
  54. }
  55. if ($this->isWindows()) {
  56. // @codeCoverageIgnoreStart
  57. return (defined('STDOUT') && function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT)) ||
  58. false !== getenv('ANSICON') ||
  59. 'ON' === getenv('ConEmuANSI') ||
  60. 'xterm' === getenv('TERM');
  61. // @codeCoverageIgnoreEnd
  62. }
  63. if (!defined('STDOUT')) {
  64. // @codeCoverageIgnoreStart
  65. return false;
  66. // @codeCoverageIgnoreEnd
  67. }
  68. return $this->isInteractive(STDOUT);
  69. }
  70. /**
  71. * Returns the number of columns of the terminal.
  72. *
  73. * @codeCoverageIgnore
  74. */
  75. public function getNumberOfColumns(): int
  76. {
  77. if (!$this->isInteractive(defined('STDIN') ? STDIN : self::STDIN)) {
  78. return 80;
  79. }
  80. if ($this->isWindows()) {
  81. return $this->getNumberOfColumnsWindows();
  82. }
  83. return $this->getNumberOfColumnsInteractive();
  84. }
  85. /**
  86. * Returns if the file descriptor is an interactive terminal or not.
  87. *
  88. * Normally, we want to use a resource as a parameter, yet sadly it's not always available,
  89. * eg when running code in interactive console (`php -a`), STDIN/STDOUT/STDERR constants are not defined.
  90. *
  91. * @param int|resource $fileDescriptor
  92. */
  93. public function isInteractive($fileDescriptor = self::STDOUT): bool
  94. {
  95. if (is_resource($fileDescriptor)) {
  96. if (function_exists('stream_isatty') && @stream_isatty($fileDescriptor)) {
  97. return true;
  98. }
  99. if (function_exists('fstat')) {
  100. $stat = @fstat(STDOUT);
  101. return $stat && 0o020000 === ($stat['mode'] & 0o170000);
  102. }
  103. return false;
  104. }
  105. return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
  106. }
  107. private function isWindows(): bool
  108. {
  109. return DIRECTORY_SEPARATOR === '\\';
  110. }
  111. /**
  112. * @codeCoverageIgnore
  113. */
  114. private function getNumberOfColumnsInteractive(): int
  115. {
  116. if (function_exists('shell_exec') && preg_match('#\d+ (\d+)#', shell_exec('stty size') ?: '', $match) === 1) {
  117. if ((int) $match[1] > 0) {
  118. return (int) $match[1];
  119. }
  120. }
  121. if (function_exists('shell_exec') && preg_match('#columns = (\d+);#', shell_exec('stty') ?: '', $match) === 1) {
  122. if ((int) $match[1] > 0) {
  123. return (int) $match[1];
  124. }
  125. }
  126. return 80;
  127. }
  128. /**
  129. * @codeCoverageIgnore
  130. */
  131. private function getNumberOfColumnsWindows(): int
  132. {
  133. $ansicon = getenv('ANSICON');
  134. $columns = 80;
  135. if (is_string($ansicon) && preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim($ansicon), $matches)) {
  136. $columns = (int) $matches[1];
  137. } elseif (function_exists('proc_open')) {
  138. $process = proc_open(
  139. 'mode CON',
  140. [
  141. 1 => ['pipe', 'w'],
  142. 2 => ['pipe', 'w'],
  143. ],
  144. $pipes,
  145. null,
  146. null,
  147. ['suppress_errors' => true],
  148. );
  149. if (is_resource($process)) {
  150. $info = stream_get_contents($pipes[1]);
  151. fclose($pipes[1]);
  152. fclose($pipes[2]);
  153. proc_close($process);
  154. if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
  155. $columns = (int) $matches[2];
  156. }
  157. }
  158. }
  159. return $columns - 1;
  160. }
  161. }