-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
0ae2de8
commit 81b58bb
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |