From 81b58bb543db10907b2529e49f7ca9e9d38fdfff Mon Sep 17 00:00:00 2001 From: Allan Mariucci Carvalho Date: Wed, 21 Feb 2024 16:25:08 -0300 Subject: [PATCH] Added pause prompt (#108) * Added pause prompt * refactoring * Ignore when non-interactive * Update helpers.php --------- Co-authored-by: Jess Archer Co-authored-by: Taylor Otwell --- playground/pause.php | 9 ++++ src/Concerns/Themes.php | 3 ++ src/PausePrompt.php | 27 +++++++++++ src/Themes/Default/PausePromptRenderer.php | 24 ++++++++++ src/helpers.php | 8 ++++ tests/Feature/PausePromptTest.php | 52 ++++++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 playground/pause.php create mode 100644 src/PausePrompt.php create mode 100644 src/Themes/Default/PausePromptRenderer.php create mode 100644 tests/Feature/PausePromptTest.php diff --git a/playground/pause.php b/playground/pause.php new file mode 100644 index 00000000..d10c1c17 --- /dev/null +++ b/playground/pause.php @@ -0,0 +1,9 @@ + 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, diff --git a/src/PausePrompt.php b/src/PausePrompt.php new file mode 100644 index 00000000..65605d58 --- /dev/null +++ b/src/PausePrompt.php @@ -0,0 +1,27 @@ +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; + } +} diff --git a/src/Themes/Default/PausePromptRenderer.php b/src/Themes/Default/PausePromptRenderer.php new file mode 100644 index 00000000..242be178 --- /dev/null +++ b/src/Themes/Default/PausePromptRenderer.php @@ -0,0 +1,24 @@ +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; + } +} diff --git a/src/helpers.php b/src/helpers.php index d5e60b68..8c05b784 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -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. * diff --git a/tests/Feature/PausePromptTest.php b/tests/Feature/PausePromptTest.php new file mode 100644 index 00000000..66b1bd7b --- /dev/null +++ b/tests/Feature/PausePromptTest.php @@ -0,0 +1,52 @@ +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'); +});