Terminal.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. class Terminal
  12. {
  13. private static $width;
  14. private static $height;
  15. private static $stty;
  16. /**
  17. * Gets the terminal width.
  18. *
  19. * @return int
  20. */
  21. public function getWidth()
  22. {
  23. $width = getenv('COLUMNS');
  24. if (false !== $width) {
  25. return (int) trim($width);
  26. }
  27. if (null === self::$width) {
  28. self::initDimensions();
  29. }
  30. return self::$width ?: 80;
  31. }
  32. /**
  33. * Gets the terminal height.
  34. *
  35. * @return int
  36. */
  37. public function getHeight()
  38. {
  39. $height = getenv('LINES');
  40. if (false !== $height) {
  41. return (int) trim($height);
  42. }
  43. if (null === self::$height) {
  44. self::initDimensions();
  45. }
  46. return self::$height ?: 50;
  47. }
  48. /**
  49. * @internal
  50. */
  51. public static function hasSttyAvailable(): bool
  52. {
  53. if (null !== self::$stty) {
  54. return self::$stty;
  55. }
  56. // skip check if shell_exec function is disabled
  57. if (!\function_exists('shell_exec')) {
  58. return false;
  59. }
  60. return self::$stty = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
  61. }
  62. private static function initDimensions()
  63. {
  64. if ('\\' === \DIRECTORY_SEPARATOR) {
  65. $ansicon = getenv('ANSICON');
  66. if (false !== $ansicon && preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim($ansicon), $matches)) {
  67. // extract [w, H] from "wxh (WxH)"
  68. // or [w, h] from "wxh"
  69. self::$width = (int) $matches[1];
  70. self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
  71. } elseif (!self::hasVt100Support() && self::hasSttyAvailable()) {
  72. // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
  73. // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
  74. self::initDimensionsUsingStty();
  75. } elseif (null !== $dimensions = self::getConsoleMode()) {
  76. // extract [w, h] from "wxh"
  77. self::$width = (int) $dimensions[0];
  78. self::$height = (int) $dimensions[1];
  79. }
  80. } else {
  81. self::initDimensionsUsingStty();
  82. }
  83. }
  84. /**
  85. * Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
  86. */
  87. private static function hasVt100Support(): bool
  88. {
  89. return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w'));
  90. }
  91. /**
  92. * Initializes dimensions using the output of an stty columns line.
  93. */
  94. private static function initDimensionsUsingStty()
  95. {
  96. if ($sttyString = self::getSttyColumns()) {
  97. if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
  98. // extract [w, h] from "rows h; columns w;"
  99. self::$width = (int) $matches[2];
  100. self::$height = (int) $matches[1];
  101. } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
  102. // extract [w, h] from "; h rows; w columns"
  103. self::$width = (int) $matches[2];
  104. self::$height = (int) $matches[1];
  105. }
  106. }
  107. }
  108. /**
  109. * Runs and parses mode CON if it's available, suppressing any error output.
  110. *
  111. * @return int[]|null An array composed of the width and the height or null if it could not be parsed
  112. */
  113. private static function getConsoleMode(): ?array
  114. {
  115. $info = self::readFromProcess('mode CON');
  116. if (null === $info || !preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
  117. return null;
  118. }
  119. return [(int) $matches[2], (int) $matches[1]];
  120. }
  121. /**
  122. * Runs and parses stty -a if it's available, suppressing any error output.
  123. */
  124. private static function getSttyColumns(): ?string
  125. {
  126. return self::readFromProcess('stty -a | grep columns');
  127. }
  128. private static function readFromProcess(string $command): ?string
  129. {
  130. if (!\function_exists('proc_open')) {
  131. return null;
  132. }
  133. $descriptorspec = [
  134. 1 => ['pipe', 'w'],
  135. 2 => ['pipe', 'w'],
  136. ];
  137. $cp = \function_exists('sapi_windows_cp_set') ? sapi_windows_cp_get() : 0;
  138. $process = proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
  139. if (!\is_resource($process)) {
  140. return null;
  141. }
  142. $info = stream_get_contents($pipes[1]);
  143. fclose($pipes[1]);
  144. fclose($pipes[2]);
  145. proc_close($process);
  146. if ($cp) {
  147. sapi_windows_cp_set($cp);
  148. }
  149. return $info;
  150. }
  151. }