Every command can read and write from and to the terminal by injecting the Console
interface. You don't have to configure anything, Tempests takes care of injecting the right dependencies for you behind the scenes:
use Tempest\Console\Console; final readonly class Package { public function __construct( private Console $console, ) {} #[ConsoleCommand] public function all(): void {} }
The Console
interface has a bunch of methods you can use:
readln(): string
— read one lineread(int $bytes): string
— read a specific amount of byteswrite(string $contents): Console
— write some text to the terminalwriteln(string $line = ''): Console
— write a line to the terminalask(string $question, ?array $options = null, bool $multiple = false, array $validation = []): string
— interactively ask a questionconfirm(string $question, bool $default = false): bool
— confirm with yes or nopassword(string $label = 'Password', bool $confirm = false): string
— retrieve a password, only works on terminals that support interactive componentsprogressBar(iterable $data, Closure $handler): array
— show a progress bar while doing some workinfo(string $line): Console
— write an info styled line to the terminalerror(string $line): Console
— write an error styled line to the terminalsuccess(string $line): Console
— write a success styled line to the terminalwhen(mixed $expression, callable $callback): Console
— only perform an action when a condition is met. The console is passed to the callable.component(ConsoleComponent $component, array $validation = []): mixed
— render a console component.This packages uses tempest/highlight
to style console output. You can use HTML-like tags to style the output like so:
$this->console->writeln("<success>{$line}</success>");
The following tags are available:
<em>Emphasize</em>
results in Emphasize
<strong>Strong</strong>
results in Strong
<u>Underline</u>
results in Underline
<h1>Header 1</h1>
results in Header 1
<h2>Header 2</h2>
results in Header 2
<question>Question</question>
results in Question
<error>Error</error>
results in Error
<success>Success</success>
results in Success
<comment>Comment</comment>
results in /* Comment */