Skip to content

Commit

Permalink
Merge branch 'laravel:main' into feature/multi-line-input
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum authored Mar 5, 2024
2 parents 3ff9af4 + 73abd37 commit 8fe583b
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/1_Bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: "Report something that's broken."
body:
- type: markdown
attributes:

value: "Please read [our full contribution guide](https://laravel.com/docs/contributions#bug-reports) before submitting bug reports. If you notice improper DocBlock, PHPStan, or IDE warnings while using Laravel, do not create a GitHub issue. Instead, please submit a pull request to fix the problem." - type: input
value: "Please read [our full contribution guide](https://laravel.com/docs/contributions#bug-reports) before submitting bug reports. If you notice improper DocBlock, PHPStan, or IDE warnings while using Laravel, do not create a GitHub issue. Instead, please submit a pull request to fix the problem."
- type: input
attributes:
label: Laravel Prompts Version
description: Provide the Laravel Prompts version that you are using.
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Release Notes

## [Unreleased](https://github.com/laravel/prompts/compare/v0.1.15...main)
## [Unreleased](https://github.com/laravel/prompts/compare/v0.1.16...main)

## [v0.1.16](https://github.com/laravel/prompts/compare/v0.1.15...v0.1.16) - 2024-02-21

* [0.x] Fix `multisearch` long option truncation by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/prompts/pull/114
* Added pause prompt by [@allanmcarvalho](https://github.com/allanmcarvalho) in https://github.com/laravel/prompts/pull/108

## [v0.1.15](https://github.com/laravel/prompts/compare/v0.1.14...v0.1.15) - 2023-12-29

Expand Down
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 @@ -21,6 +22,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 @@ -50,6 +52,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
28 changes: 28 additions & 0 deletions src/PausePrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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;
}
}
2 changes: 1 addition & 1 deletion src/Themes/Default/MultiSearchPromptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function renderOptions(MultiSearchPrompt $prompt): string

return $this->scrollbar(
collect($prompt->visible())
->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 10))
->map(fn ($label) => $this->truncate($label, $prompt->terminal()->cols() - 12))
->map(function ($label, $key) use ($prompt) {
$index = array_search($key, array_keys($prompt->matches()));
$active = $index === $prompt->highlighted;
Expand Down
25 changes: 25 additions & 0 deletions src/Themes/Default/PausePromptRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 @@ -60,6 +60,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 8fe583b

Please sign in to comment.