No Description

Terminal.php 4.9KB

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