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