Terminal.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  16. * Gets the terminal width.
  17. *
  18. * @return int
  19. */
  20. public function getWidth()
  21. {
  22. $width = getenv('COLUMNS');
  23. if (false !== $width) {
  24. return (int) trim($width);
  25. }
  26. if (null === self::$width) {
  27. self::initDimensions();
  28. }
  29. return self::$width ?: 80;
  30. }
  31. /**
  32. * Gets the terminal height.
  33. *
  34. * @return int
  35. */
  36. public function getHeight()
  37. {
  38. $height = getenv('LINES');
  39. if (false !== $height) {
  40. return (int) trim($height);
  41. }
  42. if (null === self::$height) {
  43. self::initDimensions();
  44. }
  45. return self::$height ?: 50;
  46. }
  47. private static function initDimensions()
  48. {
  49. if ('\\' === \DIRECTORY_SEPARATOR) {
  50. if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
  51. // extract [w, H] from "wxh (WxH)"
  52. // or [w, h] from "wxh"
  53. self::$width = (int) $matches[1];
  54. self::$height = isset($matches[4]) ? (int) $matches[4] : (int) $matches[2];
  55. } elseif (null !== $dimensions = self::getConsoleMode()) {
  56. // extract [w, h] from "wxh"
  57. self::$width = (int) $dimensions[0];
  58. self::$height = (int) $dimensions[1];
  59. }
  60. } elseif ($sttyString = self::getSttyColumns()) {
  61. if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
  62. // extract [w, h] from "rows h; columns w;"
  63. self::$width = (int) $matches[2];
  64. self::$height = (int) $matches[1];
  65. } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
  66. // extract [w, h] from "; h rows; w columns"
  67. self::$width = (int) $matches[2];
  68. self::$height = (int) $matches[1];
  69. }
  70. }
  71. }
  72. /**
  73. * Runs and parses mode CON if it's available, suppressing any error output.
  74. *
  75. * @return int[]|null An array composed of the width and the height or null if it could not be parsed
  76. */
  77. private static function getConsoleMode()
  78. {
  79. if (!\function_exists('proc_open')) {
  80. return;
  81. }
  82. $descriptorspec = [
  83. 1 => ['pipe', 'w'],
  84. 2 => ['pipe', 'w'],
  85. ];
  86. $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
  87. if (\is_resource($process)) {
  88. $info = stream_get_contents($pipes[1]);
  89. fclose($pipes[1]);
  90. fclose($pipes[2]);
  91. proc_close($process);
  92. if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
  93. return [(int) $matches[2], (int) $matches[1]];
  94. }
  95. }
  96. }
  97. /**
  98. * Runs and parses stty -a if it's available, suppressing any error output.
  99. *
  100. * @return string|null
  101. */
  102. private static function getSttyColumns()
  103. {
  104. if (!\function_exists('proc_open')) {
  105. return;
  106. }
  107. $descriptorspec = [
  108. 1 => ['pipe', 'w'],
  109. 2 => ['pipe', 'w'],
  110. ];
  111. $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]);
  112. if (\is_resource($process)) {
  113. $info = stream_get_contents($pipes[1]);
  114. fclose($pipes[1]);
  115. fclose($pipes[2]);
  116. proc_close($process);
  117. return $info;
  118. }
  119. }
  120. }