Skip to content

Commit

Permalink
Added pause prompt (#108)
Browse files Browse the repository at this point in the history
* Added pause prompt

* refactoring

* Ignore when non-interactive

* Update helpers.php

---------

Co-authored-by: Jess Archer <jess@jessarcher.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent 0ae2de8 commit 81b58bb
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions playground/pause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use function Laravel\Prompts\pause;

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

$continued = pause();

var_dump($continued);
3 changes: 3 additions & 0 deletions src/Concerns/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Prompts\MultiSelectPrompt;
use Laravel\Prompts\Note;
use Laravel\Prompts\PasswordPrompt;
use Laravel\Prompts\PausePrompt;
use Laravel\Prompts\Progress;
use Laravel\Prompts\SearchPrompt;
use Laravel\Prompts\SelectPrompt;
Expand All @@ -20,6 +21,7 @@
use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer;
use Laravel\Prompts\Themes\Default\NoteRenderer;
use Laravel\Prompts\Themes\Default\PasswordPromptRenderer;
use Laravel\Prompts\Themes\Default\PausePromptRenderer;
use Laravel\Prompts\Themes\Default\ProgressRenderer;
use Laravel\Prompts\Themes\Default\SearchPromptRenderer;
use Laravel\Prompts\Themes\Default\SelectPromptRenderer;
Expand Down Expand Up @@ -47,6 +49,7 @@ trait Themes
SelectPrompt::class => SelectPromptRenderer::class,
MultiSelectPrompt::class => MultiSelectPromptRenderer::class,
ConfirmPrompt::class => ConfirmPromptRenderer::class,
PausePrompt::class => PausePromptRenderer::class,
SearchPrompt::class => SearchPromptRenderer::class,
MultiSearchPrompt::class => MultiSearchPromptRenderer::class,
SuggestPrompt::class => SuggestPromptRenderer::class,
Expand Down
27 changes: 27 additions & 0 deletions src/PausePrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Laravel\Prompts;

class PausePrompt extends Prompt
{
/**
* Create a new PausePrompt instance.
*/
public function __construct(public string $message = 'Press enter to continue...') {
$this->required = false;
$this->validate = null;

$this->on('key', fn ($key) => match($key) {
Key::ENTER => $this->submit(),
default => null,
});
}

/**
* Get the value of the prompt.
*/
public function value(): bool
{
return static::$interactive;
}
}
24 changes: 24 additions & 0 deletions src/Themes/Default/PausePromptRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\PausePrompt;

class PausePromptRenderer extends Renderer
{
use Concerns\DrawsBoxes;

/**
* Render the pause prompt.
*/
public function __invoke(PausePrompt $prompt): string
{
match ($prompt->state) {
'submit' => collect(explode(PHP_EOL, $prompt->message))
->each(fn($line) => $this->line($this->gray(" {$line}"))),
default => collect(explode(PHP_EOL, $prompt->message))
->each(fn($line) => $this->line($this->green(" {$line}")))
};
return $this;
}
}
8 changes: 8 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ function confirm(string $label, bool $default = true, string $yes = 'Yes', strin
return (new ConfirmPrompt(...func_get_args()))->prompt();
}

/**
* Prompt the user to continue or cancel after pausing.
*/
function pause(string $message = 'Press enter to continue...'): bool
{
return (new PausePrompt(...func_get_args()))->prompt();
}

/**
* Prompt the user for text input with auto-completion.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/Feature/PausePromptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Laravel\Prompts\Key;
use Laravel\Prompts\PausePrompt;
use Laravel\Prompts\Prompt;

use function Laravel\Prompts\pause;

it('continues after enter', function () {
Prompt::fake([Key::ENTER]);

$result = pause();

expect($result)->toBeTrue();

Prompt::assertOutputContains('Press enter to continue...');
});

it('allows the message to be changed', function () {
Prompt::fake([Key::ENTER]);

$result = pause('Read and then press enter...');

expect($result)->toBeTrue();

Prompt::assertOutputContains('Read and then press enter...');
});

it('can fall back', function () {
Prompt::fallbackWhen(true);

PausePrompt::fallbackUsing(function (PausePrompt $prompt) {
expect($prompt->message)->toBe('Press enter to continue...');

return true;
});

$result = pause();

expect($result)->toBeTrue();
});

it('does not render when non-interactive', function () {
Prompt::fake();
Prompt::interactive(false);

$result = pause('This should not be rendered');

expect($result)->toBeFalse();

Prompt::assertOutputDoesntContain('This should not be rendered');
});

0 comments on commit 81b58bb

Please sign in to comment.