From 6d601bf31e761ef3f61115102e7c8c9b709214dc Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 17 Sep 2024 16:52:18 -0700 Subject: [PATCH 1/5] tell phpunit to fail on E_WARNING --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index cc33dea5..9520ed4e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./tests From 89d4437da25feba6db4a4f18605d32574dc47d03 Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 17 Sep 2024 16:52:30 -0700 Subject: [PATCH 2/5] add test --- tests/Feature/TextPromptTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Feature/TextPromptTest.php b/tests/Feature/TextPromptTest.php index f11ac538..dcd7ef18 100644 --- a/tests/Feature/TextPromptTest.php +++ b/tests/Feature/TextPromptTest.php @@ -159,3 +159,11 @@ text('What is your name?'); })->throws(Exception::class, 'Cancelled.'); + +it('handles a failed terminal read gracefully', function () { + Prompt::fake(['', Key::ENTER]); + + $result = text('What is your name?'); + + expect($result)->toBe(''); +}); From d66f6378c7b45d8205e89810219104a445a2040a Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 17 Sep 2024 16:57:48 -0700 Subject: [PATCH 3/5] check for empty string in TypedValue listener --- src/Concerns/TypedValue.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Concerns/TypedValue.php b/src/Concerns/TypedValue.php index 56d356ad..65544190 100644 --- a/src/Concerns/TypedValue.php +++ b/src/Concerns/TypedValue.php @@ -28,7 +28,9 @@ protected function trackTypedValue(string $default = '', bool $submit = true, ?c } $this->on('key', function ($key) use ($submit, $ignore, $allowNewLine) { - if ($key[0] === "\e" || in_array($key, [Key::CTRL_B, Key::CTRL_F, Key::CTRL_A, Key::CTRL_E])) { + if ($key !== '' && + ($key[0] === "\e" || in_array($key, [Key::CTRL_B, Key::CTRL_F, Key::CTRL_A, Key::CTRL_E])) + ) { if ($ignore !== null && $ignore($key)) { return; } From 86720e2f494b00ca44e5f2966359f0a39997e0da Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 17 Sep 2024 16:58:16 -0700 Subject: [PATCH 4/5] strengthen types --- src/Concerns/TypedValue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/TypedValue.php b/src/Concerns/TypedValue.php index 65544190..c4cddbd6 100644 --- a/src/Concerns/TypedValue.php +++ b/src/Concerns/TypedValue.php @@ -27,7 +27,7 @@ protected function trackTypedValue(string $default = '', bool $submit = true, ?c $this->cursorPosition = mb_strlen($this->typedValue); } - $this->on('key', function ($key) use ($submit, $ignore, $allowNewLine) { + $this->on('key', function (string $key) use ($submit, $ignore, $allowNewLine): void { if ($key !== '' && ($key[0] === "\e" || in_array($key, [Key::CTRL_B, Key::CTRL_F, Key::CTRL_A, Key::CTRL_E])) ) { From fa48413fbbe09e548130854b51ef7159f351e793 Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 17 Sep 2024 16:58:40 -0700 Subject: [PATCH 5/5] continue loop is key is empty string --- src/Prompt.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Prompt.php b/src/Prompt.php index 3168f2e9..324d7899 100644 --- a/src/Prompt.php +++ b/src/Prompt.php @@ -167,6 +167,15 @@ public function prompt(): mixed public function runLoop(callable $callable): mixed { while (($key = static::terminal()->read()) !== null) { + /** + * If $key is an empty string, Terminal::read + * has failed. We can continue to the next + * iteration of the loop, and try again. + */ + if ($key === '') { + continue; + } + $result = $callable($key); if ($result instanceof Result) {