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'); +});