Skip to content

Commit

Permalink
Add support for alternative arrow keys (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored Aug 7, 2023
1 parent a7fc438 commit e65a276
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/TypedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected function trackTypedValue(string $default = '', bool $submit = true): v
$this->on('key', function ($key) use ($submit) {
if ($key[0] === "\e") {
match ($key) {
Key::LEFT => $this->cursorPosition = max(0, $this->cursorPosition - 1),
Key::RIGHT => $this->cursorPosition = min(mb_strlen($this->typedValue), $this->cursorPosition + 1),
Key::LEFT, Key::LEFT_ARROW => $this->cursorPosition = max(0, $this->cursorPosition - 1),
Key::RIGHT, Key::RIGHT_ARROW => $this->cursorPosition = min(mb_strlen($this->typedValue), $this->cursorPosition + 1),
Key::DELETE => $this->typedValue = mb_substr($this->typedValue, 0, $this->cursorPosition).mb_substr($this->typedValue, $this->cursorPosition + 1),
default => null,
};
Expand Down
2 changes: 1 addition & 1 deletion src/ConfirmPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
'y' => $this->confirmed = true,
'n' => $this->confirmed = false,
Key::TAB, Key::UP, Key::DOWN, Key::LEFT, Key::RIGHT, 'h', 'j', 'k', 'l' => $this->confirmed = ! $this->confirmed,
Key::TAB, Key::UP, Key::UP_ARROW, Key::DOWN, Key::DOWN_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW, 'h', 'j', 'k', 'l' => $this->confirmed = ! $this->confirmed,
Key::ENTER => $this->submit(),
default => null,
});
Expand Down
8 changes: 8 additions & 0 deletions src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class Key

const LEFT = "\e[D";

const UP_ARROW = "\eOA";

const DOWN_ARROW = "\eOB";

const RIGHT_ARROW = "\eOC";

const LEFT_ARROW = "\eOD";

const DELETE = "\e[3~";

const BACKSPACE = "\177";
Expand Down
4 changes: 2 additions & 2 deletions src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function __construct(
$this->values = $this->default;

$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::LEFT, Key::SHIFT_TAB, 'k', 'h' => $this->highlightPrevious(),
Key::DOWN, Key::RIGHT, Key::TAB, 'j', 'l' => $this->highlightNext(),
Key::UP, Key::UP_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::SHIFT_TAB, 'k', 'h' => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::TAB, 'j', 'l' => $this->highlightNext(),
Key::SPACE => $this->toggleHighlighted(),
Key::ENTER => $this->submit(),
default => null,
Expand Down
6 changes: 3 additions & 3 deletions src/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function __construct(
$this->trackTypedValue(submit: false);

$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::TAB => $this->highlightNext(),
Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(),
Key::ENTER => $this->highlighted !== null ? $this->submit() : $this->search(),
Key::LEFT, Key::RIGHT => $this->highlighted = null,
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
default => $this->search(),
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function __construct(
}

$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::LEFT, Key::SHIFT_TAB, 'k', 'h' => $this->highlightPrevious(),
Key::DOWN, Key::RIGHT, Key::TAB, 'j', 'l' => $this->highlightNext(),
Key::UP, Key::UP_ARROW, Key::LEFT, Key::LEFT_ARROW, Key::SHIFT_TAB, 'k', 'h' => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::RIGHT, Key::RIGHT_ARROW, Key::TAB, 'j', 'l' => $this->highlightNext(),
Key::ENTER => $this->submit(),
default => null,
});
Expand Down
6 changes: 3 additions & 3 deletions src/SuggestPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function __construct(
$this->options = $options instanceof Collection ? $options->all() : $options;

$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::TAB => $this->highlightNext(),
Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(),
Key::ENTER => $this->selectHighlighted(),
Key::LEFT, Key::RIGHT => $this->highlighted = null,
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
default => (function () {
$this->highlighted = null;
$this->matches = null;
Expand Down

0 comments on commit e65a276

Please sign in to comment.