Skip to content

Add Page-Up and Page-Down key for list prompt #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Key

const LEFT = "\e[D";

const PAGE_UP = "\e[5~";

const PAGE_DOWN = "\e[6~";

const UP_ARROW = "\eOA";

const DOWN_ARROW = "\eOB";
Expand Down
34 changes: 34 additions & 0 deletions src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
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::PAGE_UP => $this->highlightPreviousPage(),
Key::PAGE_DOWN => $this->highlightNextPage(),
Key::SPACE => $this->toggleHighlighted(),
Key::ENTER => $this->submit(),
default => null,
Expand Down Expand Up @@ -152,6 +154,38 @@ protected function highlightNext(): void
}
}

/**
* Highlight the previous page entry, or wrap around to the last entry.
*/
protected function highlightPreviousPage(): void
{
$this->highlighted = $this->highlighted === 0 ? count($this->options) - 1 : max($this->highlighted - $this->scroll, 0);

if ($this->highlighted === count($this->options) - 1) {
$this->firstVisible = count($this->options) - min($this->scroll, count($this->options));
} elseif ($this->highlighted === 0) {
$this->firstVisible = 0;
} elseif ($this->highlighted < $this->firstVisible) {
$this->firstVisible = max($this->firstVisible - $this->scroll, 0);
}
}

/**
* Highlight the next page entry, or wrap around to the first entry.
*/
protected function highlightNextPage(): void
{
$this->highlighted = $this->highlighted === count($this->options) - 1 ? 0 : min($this->highlighted + $this->scroll, count($this->options) - 1);

if ($this->highlighted === 0) {
$this->firstVisible = 0;
} elseif ($this->highlighted === count($this->options) - 1) {
$this->firstVisible = count($this->options) - min($this->scroll, count($this->options));
} elseif ($this->highlighted > $this->firstVisible + $this->scroll - 1) {
$this->firstVisible = min($this->firstVisible + $this->scroll, count($this->options) - $this->scroll);
}
}

/**
* Toggle the highlighted entry.
*/
Expand Down
50 changes: 50 additions & 0 deletions src/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(),
Key::PAGE_UP => $this->highlightPreviousPage(),
Key::PAGE_DOWN => $this->highlightNextPage(),
Key::ENTER => $this->highlighted !== null ? $this->submit() : $this->search(),
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
default => $this->search(),
Expand Down Expand Up @@ -149,6 +151,54 @@ protected function highlightNext(): void
}
}

/**
* Highlight the previous page entry, or wrap around to the last entry.
*/
protected function highlightPreviousPage(): void
{
if ($this->matches === []) {
$this->highlighted = null;
} elseif ($this->highlighted === null) {
$this->highlighted = count($this->matches) - 1;
} elseif ($this->highlighted === 0) {
$this->highlighted = null;
} else {
$this->highlighted = max($this->highlighted - $this->scroll, 0);
}

if ($this->highlighted === count($this->matches) - 1) {
$this->firstVisible = count($this->matches) - min($this->scroll, count($this->matches));
} elseif ($this->highlighted === 0 || $this->highlighted === null) {
$this->firstVisible = 0;
} elseif ($this->highlighted < $this->firstVisible) {
$this->firstVisible = max($this->firstVisible - $this->scroll, 0);
}
}

/**
* Highlight the next page entry, or wrap around to the first entry.
*/
protected function highlightNextPage(): void
{
if ($this->matches === []) {
$this->highlighted = null;
} elseif ($this->highlighted === null) {
$this->highlighted = 0;
} elseif ($this->highlighted === count($this->matches) - 1) {
$this->highlighted = null;
} else {
$this->highlighted = min($this->highlighted + $this->scroll, count($this->matches) - 1);
}

if ($this->highlighted === 0 || $this->highlighted === null) {
$this->firstVisible = 0;
} elseif ($this->highlighted === count($this->matches) - 1) {
$this->firstVisible = count($this->matches) - min($this->scroll, count($this->matches));
} elseif ($this->highlighted > $this->firstVisible + $this->scroll - 1) {
$this->firstVisible = min($this->firstVisible + $this->scroll, count($this->matches) - $this->scroll);
}
}

/**
* Get the current search query.
*/
Expand Down
35 changes: 35 additions & 0 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
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::PAGE_UP => $this->highlightPreviousPage(),
Key::PAGE_DOWN => $this->highlightNextPage(),
Key::ENTER => $this->submit(),
default => null,
});
Expand Down Expand Up @@ -138,4 +140,37 @@ protected function highlightNext(): void
$this->firstVisible = 0;
}
}

/**
* Highlight the previous page entry, or wrap around to the last entry.
*/
protected function highlightPreviousPage(): void
{
$this->highlighted = $this->highlighted === 0 ? count($this->options) - 1 : max($this->highlighted - $this->scroll, 0);

if ($this->highlighted === count($this->options) - 1) {
$this->firstVisible = count($this->options) - min($this->scroll, count($this->options));
} elseif ($this->highlighted === 0) {
$this->firstVisible = 0;
// 2 3
} elseif ($this->highlighted < $this->firstVisible) {
$this->firstVisible = max($this->firstVisible - $this->scroll, 0);
}
}

/**
* Highlight the next page entry, or wrap around to the first entry.
*/
protected function highlightNextPage(): void
{
$this->highlighted = $this->highlighted === count($this->options) - 1 ? 0 : min($this->highlighted + $this->scroll, count($this->options) - 1);

if ($this->highlighted === 0) {
$this->firstVisible = 0;
} elseif ($this->highlighted === count($this->options) - 1) {
$this->firstVisible = count($this->options) - min($this->scroll, count($this->options));
} elseif ($this->highlighted > $this->firstVisible + $this->scroll - 1) {
$this->firstVisible = min($this->firstVisible + $this->scroll, count($this->options) - $this->scroll);
}
}
}
50 changes: 50 additions & 0 deletions src/SuggestPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function __construct(
$this->on('key', fn ($key) => match ($key) {
Key::UP, Key::UP_ARROW, Key::SHIFT_TAB => $this->highlightPrevious(),
Key::DOWN, Key::DOWN_ARROW, Key::TAB => $this->highlightNext(),
Key::PAGE_UP => $this->highlightPreviousPage(),
Key::PAGE_DOWN => $this->highlightNextPage(),
Key::ENTER => $this->selectHighlighted(),
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
default => (function () {
Expand Down Expand Up @@ -158,6 +160,54 @@ protected function highlightNext(): void
}
}

/**
* Highlight the previous page entry, or wrap around to the last entry.
*/
protected function highlightPreviousPage(): void
{
if ($this->matches === []) {
$this->highlighted = null;
} elseif ($this->highlighted === null) {
$this->highlighted = count($this->matches) - 1;
} elseif ($this->highlighted === 0) {
$this->highlighted = null;
} else {
$this->highlighted = max($this->highlighted - $this->scroll, 0);
}

if ($this->highlighted === count($this->matches) - 1) {
$this->firstVisible = count($this->matches) - min($this->scroll, count($this->matches));
} elseif ($this->highlighted === 0 || $this->highlighted === null) {
$this->firstVisible = 0;
} elseif ($this->highlighted < $this->firstVisible) {
$this->firstVisible = max($this->firstVisible - $this->scroll, 0);
}
}

/**
* Highlight the next page entry, or wrap around to the first entry.
*/
protected function highlightNextPage(): void
{
if ($this->matches === []) {
$this->highlighted = null;
} elseif ($this->highlighted === null) {
$this->highlighted = 0;
} elseif ($this->highlighted === count($this->matches) - 1) {
$this->highlighted = null;
} else {
$this->highlighted = min($this->highlighted + $this->scroll, count($this->matches) - 1);
}

if ($this->highlighted === 0 || $this->highlighted === null) {
$this->firstVisible = 0;
} elseif ($this->highlighted === count($this->matches) - 1) {
$this->firstVisible = count($this->matches) - min($this->scroll, count($this->matches));
} elseif ($this->highlighted > $this->firstVisible + $this->scroll - 1) {
$this->firstVisible = min($this->firstVisible + $this->scroll, count($this->matches) - $this->scroll);
}
}

/**
* Select the highlighted entry.
*/
Expand Down