Skip to content

Commit

Permalink
[feat] autocomplete for questions (#153)
Browse files Browse the repository at this point in the history
* [feat] ask autocomplete

* disable tty test

* add test

* fix: Truncate to work well with `w-full` or `w-division`.

* release: v1.14.1

* chore: Add `php8.2` to the tests.

* rebase

* release: v1.14.2

* [feat] ask autocomplete

* merge with #156

* rebase

* merge with #156

* lint

Co-authored-by: Francisco Madeira <xico2k@gmail.com>
  • Loading branch information
fabio-ivona and xiCO2k authored Dec 20, 2022
1 parent 49717f9 commit 0de9989
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ function terminal(): Terminal
if (! function_exists('Termwind\ask')) {
/**
* Renders a prompt to the user.
*
* @param iterable<array-key, string>|null $autocomplete
*/
function ask(string $question): mixed
function ask(string $question, iterable $autocomplete = null): mixed
{
return (new Question)->ask($question);
return (new Question)->ask($question, $autocomplete);
}
}
14 changes: 11 additions & 3 deletions src/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ public static function getStreamableInput(): StreamableInputInterface

/**
* Renders a prompt to the user.
*
* @param iterable<array-key, string>|null $autocomplete
*/
public function ask(string $question): mixed
public function ask(string $question, iterable $autocomplete = null): mixed
{
$html = (new HtmlRenderer)->parse($question)->toString();

$question = new SymfonyQuestion($html);

if ($autocomplete !== null) {
$question->setAutocompleterValues($autocomplete);
}

$output = Termwind::getRenderer();

if ($output instanceof SymfonyStyle) {
Expand All @@ -70,7 +78,7 @@ public function ask(string $question): mixed
$property->setValue($output, new QuestionHelper);

try {
return $output->ask($html);
return $output->askQuestion($question);
} finally {
$property->setValue($output, $currentHelper);
}
Expand All @@ -79,7 +87,7 @@ public function ask(string $question): mixed
return $this->helper->ask(
self::getStreamableInput(),
Termwind::getRenderer(),
new SymfonyQuestion($html)
$question,
);
}
}
26 changes: 26 additions & 0 deletions tests/ask.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Symfony\Component\Console\Formatter\NullOutputFormatter;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use function Termwind\ask;
use Termwind\Question;
use function Termwind\renderUsing;
Expand Down Expand Up @@ -29,3 +31,27 @@
$output->shouldReceive('write')->once()->with(' <bg=red>Question</>');
$answer = ask('<span class="bg-red ml-1">Question</span>');
});

it('renders the question with autocomplete', function () {
$inputStream = getInputStream('o');
Question::setStreamableInput($input = Mockery::mock(StreamableInputInterface::class));

$input->shouldReceive('getStream')->once()->andReturn($inputStream);
$input->shouldReceive('isInteractive')->once()->andReturn(true);

renderUsing($output = Mockery::mock(OutputInterface::class));

$clearLineCode = "\x1b[K";
$savePositionCode = "\x1b7";
$restorePositionCode = "\x1b8";

$output->shouldReceive('write')->once()->with(' <bg=red>Question</>');
$output->shouldReceive('write')->once()->with('o');
$output->shouldReceive('write')->once()->with($savePositionCode);
$output->shouldReceive('write')->once()->with($clearLineCode);
$output->shouldReceive('write')->once()->with('<hl>ne</hl>');
$output->shouldReceive('write')->once()->with($restorePositionCode);
$output->shouldReceive('getFormatter')->once()->andReturn(new NullOutputFormatter());

ask('<span class="bg-red ml-1">Question</span>', ['one', 'two', 'three']);
})->skip(! Terminal::hasSttyAvailable(), '`stty` is required to test autocomplete functionality');

0 comments on commit 0de9989

Please # to comment.