-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4a558f
commit 3926f9e
Showing
6 changed files
with
233 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,38 @@ | ||
<?php | ||
|
||
use Laravel\Prompts\Prompt; | ||
use function Laravel\Prompts\pause; | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
Prompt::fallbackWhen(true); | ||
|
||
$license = "Copyright (c) 2023 Laravel LCC | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the \"Software\"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE."; | ||
|
||
$continued = pause( | ||
$license, | ||
'MIT License', | ||
hint: 'By pressing ENTER you accept the license!' | ||
); | ||
|
||
var_dump($continued); | ||
|
||
echo str_repeat(PHP_EOL, 2); |
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,46 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts; | ||
|
||
class PausePrompt extends Prompt | ||
{ | ||
/** | ||
* Whether the prompt has been confirmed. | ||
*/ | ||
public bool $confirmed = false; | ||
|
||
/** | ||
* Create a new PausePrompt instance. | ||
*/ | ||
public function __construct( | ||
public string $body, | ||
public string $title = '', | ||
public string $info = 'ENTER to continue or Ctrl+C to cancel', | ||
public bool|string $required = 'Please, press ENTER to continue or Ctrl+C to cancel.', | ||
public string $hint = '', | ||
) { | ||
$this->validate = null; | ||
$this->on('key', fn ($key) => match ($key) { | ||
default => $this->onKey($key), | ||
}); | ||
} | ||
|
||
/** | ||
* Check key pressed to allow to continue case it's enter | ||
*/ | ||
public function onKey(string $key): void | ||
{ | ||
if ($key === Key::ENTER) { | ||
$this->confirmed = true; | ||
} | ||
$this->submit(); | ||
} | ||
|
||
/** | ||
* Get the value of the prompt. | ||
*/ | ||
public function value(): bool | ||
{ | ||
return $this->confirmed; | ||
} | ||
} |
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,75 @@ | ||
<?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 | ||
{ | ||
return match ($prompt->state) { | ||
'submit' => $this | ||
->box( | ||
$this->dim($this->truncate($prompt->title, $prompt->terminal()->cols() - 6)), | ||
$this->renderBody($prompt), | ||
), | ||
|
||
'cancel' => $this | ||
->box( | ||
$this->truncate($prompt->title, $prompt->terminal()->cols() - 6), | ||
$this->renderBody($prompt), | ||
color: 'red', | ||
) | ||
->error('Cancelled.'), | ||
|
||
'error' => $this | ||
->box( | ||
$this->truncate($prompt->title, $prompt->terminal()->cols() - 6), | ||
$this->renderBody($prompt), | ||
color: 'yellow', | ||
info: $this->truncate($prompt->info, $prompt->terminal()->cols() - 15), | ||
) | ||
->warning($this->truncate($prompt->error, $prompt->terminal()->cols() - 5)), | ||
|
||
default => $this | ||
->box( | ||
$this->cyan($this->truncate($prompt->title, $prompt->terminal()->cols() - 6)), | ||
$this->renderBody($prompt), | ||
info: $this->magenta($this->truncate($prompt->info, $prompt->terminal()->cols() - 15)), | ||
) | ||
->when( | ||
$prompt->hint, | ||
fn () => $this->hint($prompt->hint), | ||
fn () => $this->newLine() // Space for errors | ||
), | ||
}; | ||
} | ||
|
||
/** | ||
* Render the pause body. | ||
*/ | ||
protected function renderBody(PausePrompt $prompt): string | ||
{ | ||
$maxRowCols = $prompt->terminal()->cols() - 6; | ||
$rows = []; | ||
$currentRow = ''; | ||
$wordsArray = explode(' ', $prompt->body); | ||
while (count($wordsArray) > 0) { | ||
$word = ' ' . $wordsArray[array_key_first($wordsArray)]; | ||
if (mb_strlen($currentRow) + mb_strlen($word) <= $maxRowCols) { | ||
$currentRow .= $word; | ||
unset($wordsArray[array_key_first($wordsArray)]); | ||
} else { | ||
$rows[] = $currentRow; | ||
$currentRow = ''; | ||
} | ||
} | ||
return implode("\n", $rows); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
use Laravel\Prompts\ConfirmPrompt; | ||
use Laravel\Prompts\Exceptions\NonInteractiveValidationException; | ||
use Laravel\Prompts\Key; | ||
use Laravel\Prompts\PausePrompt; | ||
use Laravel\Prompts\Prompt; | ||
|
||
use function Laravel\Prompts\confirm; | ||
use function Laravel\Prompts\pause; | ||
|
||
it('continues', function () { | ||
Prompt::fake([Key::ENTER]); | ||
|
||
$result = pause('This is a fake content.'); | ||
|
||
expect($result)->toBeTrue(); | ||
}); | ||
|
||
it('allows the title to be changed', function () { | ||
Prompt::fake([Key::ENTER]); | ||
|
||
$result = pause( | ||
'teste', | ||
'Leia com atenção!', | ||
); | ||
|
||
expect($result)->toBeTrue(); | ||
|
||
Prompt::assertOutputContains('Leia com atenção!'); | ||
}); | ||
|
||
|
||
it('can fall back', function () { | ||
Prompt::fallbackWhen(true); | ||
|
||
PausePrompt::fallbackUsing(function (PausePrompt $prompt) { | ||
expect($prompt->body)->toBe('This is a fake content.') | ||
->and($prompt->title) | ||
->toBe('Warning...'); | ||
|
||
return true; | ||
}); | ||
|
||
$result = pause('This is a fake content.', 'Warning...', false); | ||
|
||
expect($result)->toBeTrue(); | ||
}); | ||
|
||
it('returns the default value when non-interactive', function () { | ||
Prompt::interactive(false); | ||
pause('This is a fake content.'); | ||
})->throws(NonInteractiveValidationException::class, 'Please, press ENTER to continue or Ctrl+C to cancel.'); | ||
|
||
it('validates the default value when non-interactive', function () { | ||
Prompt::interactive(false); | ||
|
||
pause( | ||
'This is a fake content.', | ||
required: true, | ||
); | ||
})->throws(NonInteractiveValidationException::class, 'Required.'); | ||
|