diff --git a/src/mako/cli/input/Input.php b/src/mako/cli/input/Input.php index ac26de622..43c8d029b 100644 --- a/src/mako/cli/input/Input.php +++ b/src/mako/cli/input/Input.php @@ -32,6 +32,14 @@ public function read(): string return $this->reader->read(); } + /** + * Reads and returns a single character. + */ + public function readCharacter(): string + { + return $this->reader->readCharacter(); + } + /** * Returns the argument parser. */ diff --git a/src/mako/cli/input/helpers/Select.php b/src/mako/cli/input/helpers/Select.php index 6bb738575..b1d956506 100644 --- a/src/mako/cli/input/helpers/Select.php +++ b/src/mako/cli/input/helpers/Select.php @@ -10,7 +10,11 @@ use mako\cli\input\Input; use mako\cli\output\Output; +use function array_map; +use function array_search; use function array_values; +use function is_numeric; +use function mb_strtolower; use function trim; /** diff --git a/src/mako/cli/input/reader/Reader.php b/src/mako/cli/input/reader/Reader.php index e651c74df..4aaa63a45 100644 --- a/src/mako/cli/input/reader/Reader.php +++ b/src/mako/cli/input/reader/Reader.php @@ -7,6 +7,7 @@ namespace mako\cli\input\reader; +use function fgetc; use function fgets; use function trim; diff --git a/src/mako/cli/output/Cursor.php b/src/mako/cli/output/Cursor.php index 98d671d15..f5721112f 100644 --- a/src/mako/cli/output/Cursor.php +++ b/src/mako/cli/output/Cursor.php @@ -7,6 +7,11 @@ namespace mako\cli\output; +use function exec; +use function fgetc; +use function shell_exec; +use function sscanf; + /** * Cursor. */ @@ -135,6 +140,42 @@ public function moveToEndOfLine(): void $this->right(9999); } + /* + * Returns the cursor position. + * + * @return array{row: int, column: int}|null + */ + public function getPosition(): ?array + { + $settings = shell_exec('stty -g'); + + exec('stty -echo -icanon'); + + $this->output->write("\033[6n"); + + $response = ''; + + while (true) { + $char = fgetc(STDIN); + + if ($char === 'R') { + break; + } + + $response .= $char; + } + + exec("stty {$settings}"); + + sscanf($response, "\033[%d;%dR", $row, $col); + + if (isset($row, $col)) { + return ['row' => $row, 'column' => $col]; + } + + return null; + } + /** * Clears the line. */