Skip to content

Make adjusted scroll value available in prompt constructor #60

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

Merged
merged 2 commits into from
Sep 8, 2023
Merged
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
18 changes: 18 additions & 0 deletions src/Concerns/ReducesScrollingToFitTerminal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Prompts\Concerns;

use Laravel\Prompts\Themes\Contracts\Scrolling;

trait ReducesScrollingToFitTerminal
{
/**
* Reduce the scroll property to fit the terminal height.
*/
protected function reduceScrollingToFitTerminal(): void
{
$reservedLines = ($renderer = $this->getRenderer()) instanceof Scrolling ? $renderer->reservedLines() : 0;

$this->scroll = min($this->scroll, $this->terminal()->lines() - $reservedLines);
}
}
4 changes: 4 additions & 0 deletions src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class MultiSelectPrompt extends Prompt
{
use Concerns\ReducesScrollingToFitTerminal;

/**
* The index of the highlighted option.
*/
Expand Down Expand Up @@ -57,6 +59,8 @@ public function __construct(
$this->default = $default instanceof Collection ? $default->all() : $default;
$this->values = $this->default;

$this->reduceScrollingToFitTerminal();

$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(),
Expand Down
3 changes: 3 additions & 0 deletions src/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class SearchPrompt extends Prompt
{
use Concerns\ReducesScrollingToFitTerminal;
use Concerns\Truncation;
use Concerns\TypedValue;

Expand Down Expand Up @@ -41,6 +42,8 @@ public function __construct(
) {
$this->trackTypedValue(submit: false);

$this->reduceScrollingToFitTerminal();

$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(),
Expand Down
4 changes: 4 additions & 0 deletions src/SelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class SelectPrompt extends Prompt
{
use Concerns\ReducesScrollingToFitTerminal;

/**
* The index of the highlighted option.
*/
Expand Down Expand Up @@ -39,6 +41,8 @@ public function __construct(
) {
$this->options = $options instanceof Collection ? $options->all() : $options;

$this->reduceScrollingToFitTerminal();

if ($this->default) {
if (array_is_list($this->options)) {
$this->highlighted = array_search($this->default, $this->options) ?: 0;
Expand Down
3 changes: 3 additions & 0 deletions src/SuggestPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class SuggestPrompt extends Prompt
{
use Concerns\ReducesScrollingToFitTerminal;
use Concerns\Truncation;
use Concerns\TypedValue;

Expand Down Expand Up @@ -51,6 +52,8 @@ public function __construct(
) {
$this->options = $options instanceof Collection ? $options->all() : $options;

$this->reduceScrollingToFitTerminal();

$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(),
Expand Down
11 changes: 11 additions & 0 deletions src/Themes/Contracts/Scrolling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Laravel\Prompts\Themes\Contracts;

interface Scrolling
{
/**
* The number of lines to reserve outside of the scrollable area.
*/
public function reservedLines(): int;
}
13 changes: 10 additions & 3 deletions src/Themes/Default/MultiSelectPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\MultiSelectPrompt;
use Laravel\Prompts\Themes\Contracts\Scrolling;

class MultiSelectPromptRenderer extends Renderer
class MultiSelectPromptRenderer extends Renderer implements Scrolling
{
use Concerns\DrawsBoxes;
use Concerns\DrawsScrollbars;
Expand All @@ -14,8 +15,6 @@ class MultiSelectPromptRenderer extends Renderer
*/
public function __invoke(MultiSelectPrompt $prompt): string
{
$prompt->scroll = min($prompt->scroll, $prompt->terminal()->lines() - 5);

return match ($prompt->state) {
'submit' => $this
->box(
Expand Down Expand Up @@ -109,4 +108,12 @@ protected function renderSelectedOptions(MultiSelectPrompt $prompt): string
$prompt->labels()
));
}

/**
* The number of lines to reserve outside of the scrollable area.
*/
public function reservedLines(): int
{
return 5;
}
}
2 changes: 1 addition & 1 deletion src/Themes/Default/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

abstract class Renderer
{
use Truncation;
use Colors;
use Truncation;

/**
* The output to be rendered.
Expand Down
12 changes: 10 additions & 2 deletions src/Themes/Default/SearchPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\SearchPrompt;
use Laravel\Prompts\Themes\Contracts\Scrolling;

class SearchPromptRenderer extends Renderer
class SearchPromptRenderer extends Renderer implements Scrolling
{
use Concerns\DrawsBoxes;
use Concerns\DrawsScrollbars;
Expand All @@ -14,7 +15,6 @@ class SearchPromptRenderer extends Renderer
*/
public function __invoke(SearchPrompt $prompt): string
{
$prompt->scroll = min($prompt->scroll, $prompt->terminal()->lines() - 7);
$maxWidth = $prompt->terminal()->cols() - 6;

return match ($prompt->state) {
Expand Down Expand Up @@ -123,4 +123,12 @@ protected function renderOptions(SearchPrompt $prompt): string
min($this->longest($prompt->matches(), padding: 4), $prompt->terminal()->cols() - 6)
)->implode(PHP_EOL);
}

/**
* The number of lines to reserve outside of the scrollable area.
*/
public function reservedLines(): int
{
return 7;
}
}
12 changes: 10 additions & 2 deletions src/Themes/Default/SelectPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\SelectPrompt;
use Laravel\Prompts\Themes\Contracts\Scrolling;

class SelectPromptRenderer extends Renderer
class SelectPromptRenderer extends Renderer implements Scrolling
{
use Concerns\DrawsBoxes;
use Concerns\DrawsScrollbars;
Expand All @@ -14,7 +15,6 @@ class SelectPromptRenderer extends Renderer
*/
public function __invoke(SelectPrompt $prompt): string
{
$prompt->scroll = min($prompt->scroll, $prompt->terminal()->lines() - 5);
$maxWidth = $prompt->terminal()->cols() - 6;

return match ($prompt->state) {
Expand Down Expand Up @@ -83,4 +83,12 @@ protected function renderOptions(SelectPrompt $prompt): string
$prompt->state === 'cancel' ? 'dim' : 'cyan'
)->implode(PHP_EOL);
}

/**
* The number of lines to reserve outside of the scrollable area.
*/
public function reservedLines(): int
{
return 5;
}
}
12 changes: 10 additions & 2 deletions src/Themes/Default/SuggestPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\SuggestPrompt;
use Laravel\Prompts\Themes\Contracts\Scrolling;

class SuggestPromptRenderer extends Renderer
class SuggestPromptRenderer extends Renderer implements Scrolling
{
use Concerns\DrawsBoxes;
use Concerns\DrawsScrollbars;
Expand All @@ -14,7 +15,6 @@ class SuggestPromptRenderer extends Renderer
*/
public function __invoke(SuggestPrompt $prompt): string
{
$prompt->scroll = min($prompt->scroll, $prompt->terminal()->lines() - 7);
$maxWidth = $prompt->terminal()->cols() - 6;

return match ($prompt->state) {
Expand Down Expand Up @@ -111,4 +111,12 @@ protected function renderOptions(SuggestPrompt $prompt): string
$prompt->state === 'cancel' ? 'dim' : 'cyan'
)->implode(PHP_EOL);
}

/**
* The number of lines to reserve outside of the scrollable area.
*/
public function reservedLines(): int
{
return 7;
}
}