You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.5 KiB

3 years ago
  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 $output;
  18. private $input;
  19. public function __construct(OutputInterface $output, $input = null)
  20. {
  21. $this->output = $output;
  22. $this->input = $input ?? (\defined('STDIN') ? \STDIN : fopen('php://input', 'r+'));
  23. }
  24. public function moveUp(int $lines = 1): self
  25. {
  26. $this->output->write(sprintf("\x1b[%dA", $lines));
  27. return $this;
  28. }
  29. public function moveDown(int $lines = 1): self
  30. {
  31. $this->output->write(sprintf("\x1b[%dB", $lines));
  32. return $this;
  33. }
  34. public function moveRight(int $columns = 1): self
  35. {
  36. $this->output->write(sprintf("\x1b[%dC", $columns));
  37. return $this;
  38. }
  39. public function moveLeft(int $columns = 1): self
  40. {
  41. $this->output->write(sprintf("\x1b[%dD", $columns));
  42. return $this;
  43. }
  44. public function moveToColumn(int $column): self
  45. {
  46. $this->output->write(sprintf("\x1b[%dG", $column));
  47. return $this;
  48. }
  49. public function moveToPosition(int $column, int $row): self
  50. {
  51. $this->output->write(sprintf("\x1b[%d;%dH", $row + 1, $column));
  52. return $this;
  53. }
  54. public function savePosition(): self
  55. {
  56. $this->output->write("\x1b7");
  57. return $this;
  58. }
  59. public function restorePosition(): self
  60. {
  61. $this->output->write("\x1b8");
  62. return $this;
  63. }
  64. public function hide(): self
  65. {
  66. $this->output->write("\x1b[?25l");
  67. return $this;
  68. }
  69. public function show(): self
  70. {
  71. $this->output->write("\x1b[?25h\x1b[?0c");
  72. return $this;
  73. }
  74. /**
  75. * Clears all the output from the current line.
  76. */
  77. public function clearLine(): self
  78. {
  79. $this->output->write("\x1b[2K");
  80. return $this;
  81. }
  82. /**
  83. * Clears all the output from the current line after the current position.
  84. */
  85. public function clearLineAfter(): self
  86. {
  87. $this->output->write("\x1b[K");
  88. return $this;
  89. }
  90. /**
  91. * Clears all the output from the cursors' current position to the end of the screen.
  92. */
  93. public function clearOutput(): self
  94. {
  95. $this->output->write("\x1b[0J");
  96. return $this;
  97. }
  98. /**
  99. * Clears the entire screen.
  100. */
  101. public function clearScreen(): self
  102. {
  103. $this->output->write("\x1b[2J");
  104. return $this;
  105. }
  106. /**
  107. * Returns the current cursor position as x,y coordinates.
  108. */
  109. public function getCurrentPosition(): array
  110. {
  111. static $isTtySupported;
  112. if (null === $isTtySupported && \function_exists('proc_open')) {
  113. $isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
  114. }
  115. if (!$isTtySupported) {
  116. return [1, 1];
  117. }
  118. $sttyMode = shell_exec('stty -g');
  119. shell_exec('stty -icanon -echo');
  120. @fwrite($this->input, "\033[6n");
  121. $code = trim(fread($this->input, 1024));
  122. shell_exec(sprintf('stty %s', $sttyMode));
  123. sscanf($code, "\033[%d;%dR", $row, $col);
  124. return [$col, $row];
  125. }
  126. }