Terminal.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * @return bool
  52. */
  53. public static function hasSttyAvailable()
  54. {
  55. if (null !== self::$stty) {
  56. return self::$stty;
  57. }
  58. exec('stty 2>&1', $output, $exitcode);
  59. return self::$stty = 0 === $exitcode;
  60. }
  61. private static function initDimensions()
  62. {
  63. if ('\\' === \DIRECTORY_SEPARATOR) {
  64. if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
  65. // extract [w, H] from "wxh (WxH)"
  66. // or [w, h] from "wxh"
  67. self::$width = (int) $matches[1];
  68. self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
  69. } elseif (!self::hasVt100Support() && self::hasSttyAvailable()) {
  70. // only use stty on Windows if the terminal does not support vt100 (e.g. Windows 7 + git-bash)
  71. // testing for stty in a Windows 10 vt100-enabled console will implicitly disable vt100 support on STDOUT
  72. self::initDimensionsUsingStty();
  73. } elseif (null !== $dimensions = self::getConsoleMode()) {
  74. // extract [w, h] from "wxh"
  75. self::$width = (int) $dimensions[0];
  76. self::$height = (int) $dimensions[1];
  77. }
  78. } else {
  79. self::initDimensionsUsingStty();
  80. }
  81. }
  82. /**
  83. * Returns whether STDOUT has vt100 support (some Windows 10+ configurations).
  84. */
  85. private static function hasVt100Support(): bool
  86. {
  87. return \function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(fopen('php://stdout', 'w'));
  88. }
  89. /**
  90. * Initializes dimensions using the output of an stty columns line.
  91. */
  92. private static function initDimensionsUsingStty()
  93. {
  94. if ($sttyString = self::getSttyColumns()) {
  95. if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
  96. // extract [w, h] from "rows h; columns w;"
  97. self::$width = (int) $matches[2];
  98. self::$height = (int) $matches[1];
  99. } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
  100. // extract [w, h] from "; h rows; w columns"
  101. self::$width = (int) $matches[2];
  102. self::$height = (int) $matches[1];
  103. }
  104. }
  105. }
  106. /**
  107. * Runs and parses mode CON if it's available, suppressing any error output.
  108. *
  109. * @return int[]|null An array composed of the width and the height or null if it could not be parsed
  110. */
  111. private static function getConsoleMode(): ?array
  112. {
  113. $info = self::readFromProcess('mode CON');
  114. if (null === $info || !preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
  115. return null;
  116. }
  117. return [(int) $matches[2], (int) $matches[1]];
  118. }
  119. /**
  120. * Runs and parses stty -a if it's available, suppressing any error output.
  121. */
  122. private static function getSttyColumns(): ?string
  123. {
  124. return self::readFromProcess('stty -a | grep columns');
  125. }
  126. private static function readFromProcess(string $command): ?string
  127. {
  128. if (!\function_exists('proc_open')) {
  129. return null;
  130. }
  131. $descriptorspec = [
  132. 1 => ['pipe', 'w'],
  133. 2 => ['pipe', 'w'],
  134. ];
  135. $process = proc_open($command, $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
  136. if (!\is_resource($process)) {
  137. return null;
  138. }
  139. $info = stream_get_contents($pipes[1]);
  140. fclose($pipes[1]);
  141. fclose($pipes[2]);
  142. proc_close($process);
  143. return $info;
  144. }
  145. }