Skip to content

Commit

Permalink
Improve errors on unsupported environments (#20)
Browse files Browse the repository at this point in the history
* Improve error message when running on Windows

* Check that the terminal is tall enough to render the prompt
  • Loading branch information
jessarcher authored Jul 31, 2023
1 parent eeae4d7 commit 309b301
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Laravel\Prompts\Output\ConsoleOutput;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Prompt
Expand Down Expand Up @@ -77,6 +78,8 @@ public function prompt(): mixed
return $this->fallback();
}

$this->checkEnvironment();

register_shutdown_function(function () {
$this->restoreCursor();
static::terminal()->restoreTty();
Expand Down Expand Up @@ -302,4 +305,14 @@ private function validate(mixed $value): void
$this->error = $error;
}
}

/**
* Check whether the environment can support the prompt.
*/
private function checkEnvironment(): void
{
if (PHP_OS_FAMILY === 'Windows') {
throw new RuntimeException('Prompts is not currently supported on Windows. Please use WSL or configure a fallback.');
}
}
}
18 changes: 17 additions & 1 deletion src/Themes/Default/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use Laravel\Prompts\Concerns\Colors;
use Laravel\Prompts\Prompt;
use RuntimeException;

abstract class Renderer
{
Expand All @@ -20,7 +21,7 @@ abstract class Renderer
*/
public function __construct(protected Prompt $prompt)
{
//
$this->checkTerminalSize($prompt);
}

/**
Expand Down Expand Up @@ -80,4 +81,19 @@ public function __toString()
.$this->output
.(in_array($this->prompt->state, ['submit', 'cancel']) ? PHP_EOL : '');
}

/**
* Check that the terminal is large enough to render the prompt.
*/
private function checkTerminalSize(Prompt $prompt): void
{
$required = 8;
$actual = $prompt->terminal()->lines();

if ($actual < $required) {
throw new RuntimeException(
"The terminal height must be at least [$required] lines but is currently [$actual]. Please increase the height or reduce the font size."
);
}
}
}

0 comments on commit 309b301

Please sign in to comment.