Skip to content

Core\Cli\Style::of

Creates a style for text in a terminal: a colour, a background colour and attributes such as bold.

Core\Cli\Style::of takes the parts by name and returns a Core\Cli\Style. The five attributes are bold, dim, italic, underline and strikethrough. Every part is optional. Without a colour, the terminal uses its own colour. An attribute that you do not name is off.

A style alone prints nothing. You give it to Core\Cli\Text::styled together with the text. A style is a value, so you can create it once, keep it in a variable and use it for many lines. You never write an escape sequence yourself.

Good to know: the result depends on where the output goes. A terminal with fewer colours shows the nearest colour it has. Output that is not a terminal, such as a log file, gets the text with no styling.

The examples below colour one message, put several attributes on one heading, and build one style for each kind of line that a tool prints.

Options

  • colorCore\Cli\ColoroptionalThe foreground colour. Without it, the terminal's own colour is used.
  • backgroundCore\Cli\ColoroptionalThe background colour. Without it, the terminal's own background is used.
  • boldbooloptionalWhether the text is bold.
  • dimbooloptionalWhether the text is dimmed.
  • italicbooloptionalWhether the text is italic. Only some terminals show italic text.
  • underlinebooloptionalWhether the text is underlined.
  • strikethroughbooloptionalWhether the text is struck through.

Return value

Returns Core\Cli\Style. The style. Core\Cli\Text::styled draws it for the terminal this process has.

Examples

A colour for one message

<?nvs
// Shows the simplest style: one colour. This example does not run in a
// terminal, so the colour is dropped and only the text is printed.
// `Core\Cli\Color::RED` is one of the sixteen colours every terminal has.
var $red = Core\Cli\Style::of({ color: Core\Cli\Color::RED });
// `Core\Cli\Text::styled` puts the style on the text. In a terminal this line
// is red.
echo Core\Cli\Text::styled('the file was not found', $red), "\n";
// The style is a value, so the same one works for the next message.
echo Core\Cli\Text::styled('the folder was not found either', $red), "\n";
Output
the file was not found
the folder was not found either

Several attributes on one heading

<?nvs
// Shows a style with more than one part: a colour, a background colour and two
// attributes in one call. This example does not run in a terminal, so the
// styling is dropped and only the text is printed.
var $heading = Core\Cli\Style::of({
color: Core\Cli\Color::WHITE,
background: Core\Cli\Color::BLUE,
bold: true,
underline: true,
});
// An attribute you leave out is off, so this heading is not italic and not
// dimmed.
echo Core\Cli\Text::styled(' Report for March ', $heading), "\n";
// `Core\Cli\Color::rgb` reaches the colours that have no name. Terminals with
// fewer colours get the nearest one they have.
var $note = Core\Cli\Style::of({ color: Core\Cli\Color::rgb(120, 120, 120), italic: true });
echo Core\Cli\Text::styled('all numbers are in euro', $note), "\n";
Output
Report for March
all numbers are in euro

One style for each kind of line

<?nvs
// Shows the everyday use: a tool builds one style for each kind of line it
// prints and uses them all program long. This example does not run in a
// terminal, so the colours are dropped and only the text is printed.
class Report
{
// A style is a value, so the styles a program uses can live in one place.
// Each method here gives back the style for one kind of line.
public static function ok(): Core\Cli\Style
{
return Core\Cli\Style::of({ color: Core\Cli\Color::GREEN });
}
public static function warning(): Core\Cli\Style
{
return Core\Cli\Style::of({ color: Core\Cli\Color::YELLOW });
}
public static function error(): Core\Cli\Style
{
return Core\Cli\Style::of({ color: Core\Cli\Color::RED, bold: true });
}
}
array<string> $files = ['invoices.csv', 'orders.csv', 'returns.csv'];
array<string> $states = ['ok', 'warning', 'error'];
uint $at = 0;
foreach ($files as string $file) {
// The kind of the line chooses the style. The text is the same shape
// either way, which is what keeps the output readable.
string $state = $states[$at as int];
var $style = Report::ok();
if ($state == 'warning') {
$style = Report::warning();
}
if ($state == 'error') {
$style = Report::error();
}
echo Core\Cli\Text::styled(Core\Str::format('%s %s', $state, $file), $style), "\n";
$at = $at + 1;
}
Output
ok invoices.csv
warning orders.csv
error returns.csv