Skip to content

Commit

Permalink
Add multi search prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
irobin591 committed Sep 5, 2023
1 parent a4038a1 commit 5d56ca9
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 2 deletions.
43 changes: 43 additions & 0 deletions playground/multisearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use function Laravel\Prompts\multisearch;

require __DIR__.'/../vendor/autoload.php';

$models = multisearch(
label: 'Which users should receive the email?',
placeholder: 'Search...',
returnKeys: true,
options: function ($value) {
if (strlen($value) === 0) {
return [];
}

usleep(100 * 1000);

$min = min(strlen($value)-1, 10);
$max = max(10, 20 - strlen($value));

if ($max - $min < 0) {
return [];
}

$data = [];

foreach (range($min, $max) as $id) {
$data["id-$id"] = "User $id";
}

return $data;
},
validate: function ($values) {
if (in_array('id-1', $values)) {
return 'User 1 cannot receive emails';
}
},
required: true,
);

var_dump($models);

echo str_repeat(PHP_EOL, 6);
3 changes: 3 additions & 0 deletions src/Concerns/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use Laravel\Prompts\ConfirmPrompt;
use Laravel\Prompts\MultiSearchPrompt;
use Laravel\Prompts\MultiSelectPrompt;
use Laravel\Prompts\Note;
use Laravel\Prompts\PasswordPrompt;
Expand All @@ -13,6 +14,7 @@
use Laravel\Prompts\SuggestPrompt;
use Laravel\Prompts\TextPrompt;
use Laravel\Prompts\Themes\Default\ConfirmPromptRenderer;
use Laravel\Prompts\Themes\Default\MultiSearchPromptRenderer;
use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer;
use Laravel\Prompts\Themes\Default\NoteRenderer;
use Laravel\Prompts\Themes\Default\PasswordPromptRenderer;
Expand Down Expand Up @@ -42,6 +44,7 @@ trait Themes
MultiSelectPrompt::class => MultiSelectPromptRenderer::class,
ConfirmPrompt::class => ConfirmPromptRenderer::class,
SearchPrompt::class => SearchPromptRenderer::class,
MultiSearchPrompt::class => MultiSearchPromptRenderer::class,
SuggestPrompt::class => SuggestPromptRenderer::class,
Spinner::class => SpinnerRenderer::class,
Note::class => NoteRenderer::class,
Expand Down
12 changes: 10 additions & 2 deletions src/Concerns/TypedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ trait TypedValue
/**
* Track the value as the user types.
*/
protected function trackTypedValue(string $default = '', bool $submit = true): void
protected function trackTypedValue(string $default = '', bool $submit = true, ?callable $allowKey = null): void
{
$this->typedValue = $default;

if ($this->typedValue) {
$this->cursorPosition = mb_strlen($this->typedValue);
}

$this->on('key', function ($key) use ($submit) {
$this->on('key', function ($key) use ($submit, $allowKey) {
if ($key[0] === "\e") {
if ($allowKey !== null && !($allowKey)($key)) {
return;
}

match ($key) {
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),
Expand All @@ -41,6 +45,10 @@ protected function trackTypedValue(string $default = '', bool $submit = true): v

// Keys may be buffered.
foreach (mb_str_split($key) as $key) {
if ($allowKey !== null && !($allowKey)($key)) {
return;
}

if ($key === Key::ENTER && $submit) {
$this->submit();

Expand Down
231 changes: 231 additions & 0 deletions src/MultiSearchPrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<?php

namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;

class MultiSearchPrompt extends Prompt
{
use Concerns\Truncation;
use Concerns\TypedValue;

/**
* The index of the highlighted option.
*/
public ?int $highlighted = null;

/**
* The index of the first visible option.
*/
public int $firstVisible = 0;

/**
* The cached matches.
*
* @var array<int|string, string>|null
*/
protected ?array $matches = null;

/**
* The default values the multi-search prompt.
*
* @var array<int|string, string>
*/
public array $default;

/**
* The selected values.
*
* @var array<int|string, string>
*/
public array $values = [];

/**
* Create a new MultiSearchPrompt instance.
*
* @param Closure(string): array<int|string, string> $options
*/
public function __construct(
public string $label,
public Closure $options,
public bool $returnKeys = true,
array|Collection $default = [],
public string $placeholder = '',
public int $scroll = 5,
public bool|string $required = false,
public ?Closure $validate = null,
public string $hint = '',
) {
$this->default = $default instanceof Collection ? $default->all() : $default;
$this->values = $this->default;

$this->trackTypedValue(submit: false, allowKey: fn ($key) => $key !== Key::SPACE || $this->highlighted === null);

$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::SPACE => $this->highlighted !== null ? $this->toggleHighlighted() : null,
Key::ENTER => $this->submit(),
Key::LEFT, Key::LEFT_ARROW, Key::RIGHT, Key::RIGHT_ARROW => $this->highlighted = null,
default => $this->search(),
});
}

/**
* Perform the search.
*/
protected function search(): void
{
$this->state = 'searching';
$this->highlighted = null;
$this->render();
$this->matches = null;
$this->firstVisible = 0;
$this->state = 'active';
}

/**
* Get the entered value with a virtual cursor.
*/
public function valueWithCursor(int $maxWidth): string
{
if ($this->highlighted !== null) {
return $this->typedValue === ''
? $this->dim($this->truncate($this->placeholder, $maxWidth))
: $this->truncate($this->typedValue, $maxWidth);
}

if ($this->typedValue === '') {
return $this->dim($this->addCursor($this->placeholder, 0, $maxWidth));
}

return $this->addCursor($this->typedValue, $this->cursorPosition, $maxWidth);
}

/**
* Get options that match the input.
*
* @return array<string>
*/
public function matches(): array
{
if (is_array($this->matches)) {
return $this->matches;
}

if (strlen($this->typedValue) === 0) {
return $this->matches = $this->values;
}

return $this->matches = ($this->options)($this->typedValue);
}

/**
* The currently visible matches
*
* @return array<string>
*/
public function visible(): array
{
return array_slice($this->matches(), $this->firstVisible, $this->scroll, preserve_keys: true);
}

/**
* Highlight the previous entry, or wrap around to the last entry.
*/
protected function highlightPrevious(): 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 = $this->highlighted - 1;
}

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

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

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

/**
* Toggle the highlighted entry.
*/
protected function toggleHighlighted(): void
{
if ($this->returnKeys) {
$key = array_keys($this->matches)[$this->highlighted];

if (array_key_exists($key, $this->values)) {
unset($this->values[$key]);
} else {
$this->values[$key] = $this->matches[$key];
}
} else {
$value = $this->matches[$this->highlighted];

if (in_array($value, $this->values)) {
$this->values = array_filter($this->values, fn ($v) => $v !== $value);
} else {
$this->values[] = $value;
}
}
}

/**
* Get the current search query.
*/
public function searchValue(): string
{
return $this->typedValue;
}

/**
* Get the selected value.
*/
public function value(): array
{
if ($this->values === null) {
return null;
}

return $this->returnKeys
? array_keys($this->values)
: $this->values;
}

/**
* Get the selected labels.
*
* @return array<string>
*/
public function labels(): array
{
return array_values($this->values);
}
}
Loading

0 comments on commit 5d56ca9

Please sign in to comment.