quillstack / output
The library responsible for console output in CLI commands.
Requires
- php: ^8.1
Requires (Dev)
- phpstan/phpstan: ^2.0
- quillstack/unit-tests: ^0.9
README
The library responsible for console output in CLI commands, colouring text with HTML-like tags.
Installation
composer require quillstack/output
Usage
use Quillstack\Output\Output; $output = new Output(); $output->writeln('Everything <green>works</green>'); $output->writeln('Something <red>went wrong</red>');
Or through the helpers, which are there as soon as the package is installed:
writeln('Everything <green>works</green>'); write('No line ending here');
format() returns the text instead of writing it:
$line = $output->format('<yellow>Careful</yellow>');
Colours
A colour is opened with <name> and closed with </name>. A colour left open is closed
at the end of the text, so the terminal is never left painted. Tags naming no colour are
left exactly as they were, so text which merely looks like markup survives untouched.
| Tag | Tag |
|---|---|
<black> |
<dark-grey> |
<red> |
<light-red> |
<green> |
<light-green> |
<brown> |
<yellow> |
<blue> |
<light-blue> |
<purple> |
<light-purple> |
<cyan> |
<light-cyan> |
<light-grey> |
<white> |
Colours of your own go to the constructor, written as ANSI codes:
use Quillstack\Output\Colors; use Quillstack\Output\Output; $output = new Output(new Colors(['brand' => '1;35'])); $output->writeln('<brand>Quillstack</brand>');
Output which is not a terminal
Escape codes only make sense on a terminal. Piped somewhere else they are noise, so an undecorated output drops them and writes the text alone:
$output = new Output(decorated: stream_isatty(STDOUT));
Tests
composer test
Coverage needs phpdbg:
composer test:coverage
License
MIT. See LICENSE.