Terminal.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Console;
  11. use Symfony\Component\Console\Output\AnsiColorMode;
  12. class Terminal
  13. {
  14. public const DEFAULT_COLOR_MODE = AnsiColorMode::Ansi4;
  15. private static ?AnsiColorMode $colorMode = null;
  16. private static ?int $width = null;
  17. private static ?int $height = null;
  18. private static ?bool $stty = null;
  19. /**
  20. * About Ansi color types: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
  21. * For more information about true color support with terminals https://github.com/termstandard/colors/.
  22. */
  23. public static function getColorMode(): AnsiColorMode
  24. {
  25. // Use Cache from previous run (or user forced mode)
  26. if (null !== self::$colorMode) {
  27. return self::$colorMode;
  28. }
  29. // Try with $COLORTERM first
  30. if (\is_string($colorterm = getenv('COLORTERM'))) {
  31. $colorterm = strtolower($colorterm);
  32. if (str_contains($colorterm, 'truecolor')) {
  33. self::setColorMode(AnsiColorMode::Ansi24);
  34. return self::$colorMode;
  35. }
  36. if (str_contains($colorterm, '256color')) {
  37. self::setColorMode(AnsiColorMode::Ansi8);
  38. return self::$colorMode;
  39. }
  40. }
  41. // Try with $TERM
  42. if (\is_string($term = getenv('TERM'))) {
  43. $term = strtolower($term);
  44. if (str_contains($term, 'truecolor')) {
  45. self::setColorMode(AnsiColorMode::Ansi24);
  46. return self::$colorMode;
  47. }
  48. if (str_contains($term, '256color')) {
  49. self::setColorMode(AnsiColorMode::Ansi8);
  50. return self::$colorMode;
  51. }
  52. }
  53. self::setColorMode(self::DEFAULT_COLOR_MODE);
  54. return self::$colorMode;
  55. }
  56. /**
  57. * Force a terminal color mode rendering.
  58. */
  59. public static function setColorMode(?AnsiColorMode $colorMode): void
  60. {
  61. self::$colorMode = $colorMode;
  62. }
  63. /**
  64. * Gets the terminal width.
  65. */
  66. public function getWidth(): int
  67. {
  68. $width = getenv('COLUMNS');
  69. if (false !== $width) {
  70. return (int) trim($width);
  71. }
  72. if (null === self::$width) {
  73. self::initDimensions();
  74. }
  75. return self::$width ?: 80;
  76. }
  77. /**
  78. * Gets the terminal height.
  79. */
  80. public function getHeight(): int
  81. {
  82. $height = getenv('LINES');
  83. if (false !== $height) {
  84. return (int) trim($height);
  85. }
  86. if (null === self::$height) {
  87. self::initDimensions();
  88. }
  89. return self::$height ?: 50;
  90. }
  91. /**
  92. * @internal
  93. */
  94. public static function hasSttyAvailable(): bool
  95. {
  96. if (null !== self::$stty) {
  97. return self::$stty;
  98. }
  99. // skip check if shell_exec function is disabled
  100. if (!\function_exists('shell_exec')) {
  101. return false;
  102. }
  103. return self::$stty = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
  104. }
  105. private static function initDimensions(): void
  106. {
  107. if ('\\' === \DIRECTORY_SEPARATOR) {
  108. $ansicon = getenv('ANSICON');
  109. if (false !== $ansicon && preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim($ansicon), $matches)) {
  110. // extract [w, H] from "wxh (WxH)"
  111. // or [w, h] from "wxh"
  112. self::$width = (int) $matches[1];
  113. self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
  114. } elseif (!self::hasVt100Support() && self::hasSttyAvailable()) {
  115. // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
  116. // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
  117. self::initDimensionsUsingStty();
  118. } elseif (null !== $dimensions = self::getConsoleMode()) {
  119. // extract [w, h] from "wxh"
  120. self::$width = (int) $dimensions[0];
  121. self::$height = (int) $dimensions[1];
  122. }
  123. } else {
  124. self::initDimensionsUsingStty();
  125. }
  126. }
  127. /**
  128. * Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
  129. */
  130. private static function hasVt100Support(): bool
  131. {
  132. return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w'));
  133. }
  134. /**
  135. * Initializes dimensions using the output of an stty columns line.
  136. */
  137. private static function initDimensionsUsingStty(): void
  138. {
  139. if ($sttyString = self::getSttyColumns()) {
  140. if (preg_match('/rows.(\d+);.columns.(\d+);/is', $sttyString, $matches)) {
  141. // extract [w, h] from "rows h; columns w;"
  142. self::$width = (int) $matches[2];
  143. self::$height = (int) $matches[1];
  144. } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/is', $sttyString, $matches)) {
  145. // extract [w, h] from "; h rows; w columns"
  146. self::$width = (int) $matches[2];
  147. self::$height = (int) $matches[1];
  148. }
  149. }
  150. }
  151. /**
  152. * Runs and parses mode CON if it's available, suppressing any error output.
  153. *
  154. * @return int[]|null An array composed of the width and the height or null if it could not be parsed
  155. */
  156. private static function getConsoleMode(): ?array
  157. {
  158. $info = self::readFromProcess('mode CON');
  159. if (null === $info || !preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
  160. return null;
  161. }
  162. return [(int) $matches[2], (int) $matches[1]];
  163. }
  164. /**
  165. * Runs and parses stty -a if it's available, suppressing any error output.
  166. */
  167. private static function getSttyColumns(): ?string
  168. {
  169. return self::readFromProcess(['stty', '-a']);
  170. }
  171. private static function readFromProcess(string|array $command): ?string
  172. {
  173. if (!\function_exists('proc_open')) {
  174. return null;
  175. }
  176. $descriptorspec = [
  177. 1 => ['pipe', 'w'],
  178. 2 => ['pipe', 'w'],
  179. ];
  180. $cp = \function_exists('sapi_windows_cp_set') ? sapi_windows_cp_get() : 0;
  181. $process = proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
  182. if (!\is_resource($process)) {
  183. return null;
  184. }
  185. $info = stream_get_contents($pipes[1]);
  186. fclose($pipes[1]);
  187. fclose($pipes[2]);
  188. proc_close($process);
  189. if ($cp) {
  190. sapi_windows_cp_set($cp);
  191. }
  192. return $info;
  193. }
  194. }