Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Nov 3, 2024
1 parent 585943d commit 8f6723f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/mako/cli/input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/mako/cli/input/helpers/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
1 change: 1 addition & 0 deletions src/mako/cli/input/reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mako\cli\input\reader;

use function fgetc;
use function fgets;
use function trim;

Expand Down
41 changes: 41 additions & 0 deletions src/mako/cli/output/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

namespace mako\cli\output;

use function exec;
use function fgetc;
use function shell_exec;
use function sscanf;

/**
* Cursor.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 8f6723f

Please sign in to comment.