Terminal.php 4.9 KB

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