Cursor.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. use Symfony\Component\Console\Output\OutputInterface;
  12. /**
  13. * @author Pierre du Plessis <pdples@gmail.com>
  14. */
  15. final class Cursor
  16. {
  17. private OutputInterface $output;
  18. /** @var resource */
  19. private $input;
  20. /**
  21. * @param resource|null $input
  22. */
  23. public function __construct(OutputInterface $output, $input = null)
  24. {
  25. $this->output = $output;
  26. $this->input = $input ?? (\defined('STDIN') ? \STDIN : fopen('php://input', 'r+'));
  27. }
  28. /**
  29. * @return $this
  30. */
  31. public function moveUp(int $lines = 1): static
  32. {
  33. $this->output->write(sprintf("\x1b[%dA", $lines));
  34. return $this;
  35. }
  36. /**
  37. * @return $this
  38. */
  39. public function moveDown(int $lines = 1): static
  40. {
  41. $this->output->write(sprintf("\x1b[%dB", $lines));
  42. return $this;
  43. }
  44. /**
  45. * @return $this
  46. */
  47. public function moveRight(int $columns = 1): static
  48. {
  49. $this->output->write(sprintf("\x1b[%dC", $columns));
  50. return $this;
  51. }
  52. /**
  53. * @return $this
  54. */
  55. public function moveLeft(int $columns = 1): static
  56. {
  57. $this->output->write(sprintf("\x1b[%dD", $columns));
  58. return $this;
  59. }
  60. /**
  61. * @return $this
  62. */
  63. public function moveToColumn(int $column): static
  64. {
  65. $this->output->write(sprintf("\x1b[%dG", $column));
  66. return $this;
  67. }
  68. /**
  69. * @return $this
  70. */
  71. public function moveToPosition(int $column, int $row): static
  72. {
  73. $this->output->write(sprintf("\x1b[%d;%dH", $row + 1, $column));
  74. return $this;
  75. }
  76. /**
  77. * @return $this
  78. */
  79. public function savePosition(): static
  80. {
  81. $this->output->write("\x1b7");
  82. return $this;
  83. }
  84. /**
  85. * @return $this
  86. */
  87. public function restorePosition(): static
  88. {
  89. $this->output->write("\x1b8");
  90. return $this;
  91. }
  92. /**
  93. * @return $this
  94. */
  95. public function hide(): static
  96. {
  97. $this->output->write("\x1b[?25l");
  98. return $this;
  99. }
  100. /**
  101. * @return $this
  102. */
  103. public function show(): static
  104. {
  105. $this->output->write("\x1b[?25h\x1b[?0c");
  106. return $this;
  107. }
  108. /**
  109. * Clears all the output from the current line.
  110. *
  111. * @return $this
  112. */
  113. public function clearLine(): static
  114. {
  115. $this->output->write("\x1b[2K");
  116. return $this;
  117. }
  118. /**
  119. * Clears all the output from the current line after the current position.
  120. */
  121. public function clearLineAfter(): self
  122. {
  123. $this->output->write("\x1b[K");
  124. return $this;
  125. }
  126. /**
  127. * Clears all the output from the cursors' current position to the end of the screen.
  128. *
  129. * @return $this
  130. */
  131. public function clearOutput(): static
  132. {
  133. $this->output->write("\x1b[0J");
  134. return $this;
  135. }
  136. /**
  137. * Clears the entire screen.
  138. *
  139. * @return $this
  140. */
  141. public function clearScreen(): static
  142. {
  143. $this->output->write("\x1b[2J");
  144. return $this;
  145. }
  146. /**
  147. * Returns the current cursor position as x,y coordinates.
  148. */
  149. public function getCurrentPosition(): array
  150. {
  151. static $isTtySupported;
  152. if (!$isTtySupported ??= '/' === \DIRECTORY_SEPARATOR && stream_isatty(\STDOUT)) {
  153. return [1, 1];
  154. }
  155. $sttyMode = shell_exec('stty -g');
  156. shell_exec('stty -icanon -echo');
  157. @fwrite($this->input, "\033[6n");
  158. $code = trim(fread($this->input, 1024));
  159. shell_exec(sprintf('stty %s', $sttyMode));
  160. sscanf($code, "\033[%d;%dR", $row, $col);
  161. return [$col, $row];
  162. }
  163. }