| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Output\OutputInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Pierre du Plessis <[email protected]> |
| 18 |
*/ |
| 19 |
final class Cursor |
| 20 |
{ |
| 21 |
private $output; |
| 22 |
private $input; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param resource|null $input |
| 26 |
*/ |
| 27 |
public function __construct(OutputInterface $output, $input = null) |
| 28 |
{ |
| 29 |
$this->output = $output; |
| 30 |
$this->input = $input ?? (\defined('STDIN') ? \STDIN : fopen('php://input', 'r+')); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @return $this |
| 35 |
*/ |
| 36 |
public function moveUp(int $lines = 1): self |
| 37 |
{ |
| 38 |
$this->output->write(sprintf("\x1b[%dA", $lines)); |
| 39 |
|
| 40 |
return $this; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @return $this |
| 45 |
*/ |
| 46 |
public function moveDown(int $lines = 1): self |
| 47 |
{ |
| 48 |
$this->output->write(sprintf("\x1b[%dB", $lines)); |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return $this |
| 55 |
*/ |
| 56 |
public function moveRight(int $columns = 1): self |
| 57 |
{ |
| 58 |
$this->output->write(sprintf("\x1b[%dC", $columns)); |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return $this |
| 65 |
*/ |
| 66 |
public function moveLeft(int $columns = 1): self |
| 67 |
{ |
| 68 |
$this->output->write(sprintf("\x1b[%dD", $columns)); |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return $this |
| 75 |
*/ |
| 76 |
public function moveToColumn(int $column): self |
| 77 |
{ |
| 78 |
$this->output->write(sprintf("\x1b[%dG", $column)); |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return $this |
| 85 |
*/ |
| 86 |
public function moveToPosition(int $column, int $row): self |
| 87 |
{ |
| 88 |
$this->output->write(sprintf("\x1b[%d;%dH", $row + 1, $column)); |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return $this |
| 95 |
*/ |
| 96 |
public function savePosition(): self |
| 97 |
{ |
| 98 |
$this->output->write("\x1b7"); |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return $this |
| 105 |
*/ |
| 106 |
public function restorePosition(): self |
| 107 |
{ |
| 108 |
$this->output->write("\x1b8"); |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @return $this |
| 115 |
*/ |
| 116 |
public function hide(): self |
| 117 |
{ |
| 118 |
$this->output->write("\x1b[?25l"); |
| 119 |
|
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @return $this |
| 125 |
*/ |
| 126 |
public function show(): self |
| 127 |
{ |
| 128 |
$this->output->write("\x1b[?25h\x1b[?0c"); |
| 129 |
|
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Clears all the output from the current line. |
| 135 |
* |
| 136 |
* @return $this |
| 137 |
*/ |
| 138 |
public function clearLine(): self |
| 139 |
{ |
| 140 |
$this->output->write("\x1b[2K"); |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Clears all the output from the current line after the current position. |
| 147 |
*/ |
| 148 |
public function clearLineAfter(): self |
| 149 |
{ |
| 150 |
$this->output->write("\x1b[K"); |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Clears all the output from the cursors' current position to the end of the screen. |
| 157 |
* |
| 158 |
* @return $this |
| 159 |
*/ |
| 160 |
public function clearOutput(): self |
| 161 |
{ |
| 162 |
$this->output->write("\x1b[0J"); |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Clears the entire screen. |
| 169 |
* |
| 170 |
* @return $this |
| 171 |
*/ |
| 172 |
public function clearScreen(): self |
| 173 |
{ |
| 174 |
$this->output->write("\x1b[2J"); |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the current cursor position as x,y coordinates. |
| 181 |
*/ |
| 182 |
public function getCurrentPosition(): array |
| 183 |
{ |
| 184 |
static $isTtySupported; |
| 185 |
|
| 186 |
if (null === $isTtySupported && \function_exists('proc_open')) { |
| 187 |
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes); |
| 188 |
} |
| 189 |
|
| 190 |
if (!$isTtySupported) { |
| 191 |
return [1, 1]; |
| 192 |
} |
| 193 |
|
| 194 |
$sttyMode = shell_exec('stty -g'); |
| 195 |
shell_exec('stty -icanon -echo'); |
| 196 |
|
| 197 |
@fwrite($this->input, "\033[6n"); |
| 198 |
|
| 199 |
$code = trim(fread($this->input, 1024)); |
| 200 |
|
| 201 |
shell_exec(sprintf('stty %s', $sttyMode)); |
| 202 |
|
| 203 |
sscanf($code, "\033[%d;%dR", $row, $col); |
| 204 |
|
| 205 |
return [$col, $row]; |
| 206 |
} |
| 207 |
} |
| 208 |
|