Terminal.php 7.1 KB

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