Skip to content

Commit

Permalink
Merge branch 'main' into feature/configurable-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored Dec 29, 2023
2 parents 1a7d0cf + 0930ea9 commit e5eca78
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Release Notes

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

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

* Ignore new PHPStan error by [@jessarcher](https://github.com/jessarcher) in https://github.com/laravel/prompts/pull/103

## [v0.1.13](https://github.com/laravel/prompts/compare/v0.1.12...v0.1.13) - 2023-10-27

Expand Down
19 changes: 18 additions & 1 deletion src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ abstract class Prompt
*/
public mixed $validate;

/**
* The cancellation callback.
*/
protected static Closure $cancelUsing;

/**
* Indicates if the prompt has been validated.
*/
Expand Down Expand Up @@ -113,7 +118,11 @@ public function prompt(): mixed

if ($continue === false || $key === Key::CTRL_C) {
if ($key === Key::CTRL_C) {
static::terminal()->exit();
if (isset(static::$cancelUsing)) {
return (static::$cancelUsing)();
} else {
static::terminal()->exit();
}
}

return $this->value();
Expand All @@ -124,6 +133,14 @@ public function prompt(): mixed
}
}

/**
* Register a callback to be invoked when a user cancels a prompt.
*/
public static function cancelUsing(Closure $callback): void
{
static::$cancelUsing = $callback;
}

/**
* How many new lines were written by the last output.
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/TextPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,10 @@

Prompt::validateUsing(fn () => null);
});

it('allows customizing the cancellation', function () {
Prompt::cancelUsing(fn () => throw new Exception('Cancelled.'));
Prompt::fake([Key::CTRL_C]);

text('What is your name?');
})->throws(Exception::class, 'Cancelled.');

0 comments on commit e5eca78

Please sign in to comment.