PHP.GT

Coloured output

Colour support is provided through Palette and command-level output helpers.

There are two output modes:

  • One-off colour for a single message
  • Persistent palette for subsequent messages until reset

Basic usage

$this->output("Single green message", Palette::GREEN);

$this->setOutputPalette(Palette::RED, Palette::BLACK);
$this->output("This line uses red on black");
$this->output("So does this one");

$this->resetOutputPalette();
$this->output("Palette reset to terminal default");

output() writes a line and applies ANSI escape codes when a foreground/background palette is present. One-off colours reset automatically after that message, while persistent palettes remain active until resetOutputPalette().

To send coloured output to another stream, pass a StreamName enum as the fourth argument:

$this->output(
	"Error: deployment failed",
	Palette::RED,
	null,
	StreamName::ERROR
);

This makes it practical to standardise message types, for example:

  • Success: Palette::GREEN
  • Warnings: Palette::YELLOW
  • Errors: Palette::RED
  • Verbose/debug: Palette::CYAN

Next, read output streams for stream selection, or multiple commands to structure larger CLI applications.