Core\Cli\Text::styled
Core\Cli\Text::styled(
string $text,
Core\Cli\Style $style,
): Core\Cli\TextMakes terminal text that wears a style, out of a string and a Core\Cli\Style.
A style is a colour, a background colour and attributes such as bold or underline. You build one with Core\Cli\Style::of and give it to Core\Cli\Text::styled with the text it applies to. Novis writes the escape sequence the terminal at the other end understands, so you never write one yourself. Where there is no terminal, such as in a log file, the styling is dropped and the text is left.
The string is treated exactly as Core\Cli\Text::plain treats it. Every control character in it is replaced by a symbol you can see, so a file name or something a user typed is safe to colour. The result carries one escape sequence for the style, and no other one.
You print the result with echo, join it to other terminal text with +, and read the string back without the styling with text().
The examples below put a style on one message, colour a value that came from outside the program, and highlight the word a search found in each line.
Parameters
$textstringThe text to convert. The result is nottainted, for the same reason as inplain. An escape sequence in the text is replaced, so the terminal does not act on it.$styleCore\Cli\StyleThe style to use, as a value. Novis has no markup or escape syntax for styles.
Return value
Returns Core\Cli\Text. A Core\Cli\Text with the safe text between the style's escape sequence and a reset. The style is drawn for the terminal this process has. When standard output is not a terminal, there is no styling.
Examples
A style on one message
<?nvs// Shows how to put a style on a piece of text and print it. This example does// not run in a terminal, so the styling is dropped and only the text prints.
var $heading = Core\Cli\Style::of({ color: Core\Cli\Color::CYAN, bold: true });var $line = Core\Cli\Text::styled('Report for May', $heading);
// A `Core\Cli\Text` is printed with `echo`, like a string. In a terminal this// line is cyan and bold.echo $line, "\n";
// The styling is not part of the text. `text()` gives the string back, so this// prints "Report for May".echo $line->text(), "\n";Report for MayReport for MayColouring a value from outside
<?nvs// Shows colouring a value your program did not write itself. Every control// character in it is replaced by a symbol you can see. The only escape// sequence in the result is the one the style puts there.
var $chosen = Core\Cli\Style::of({ color: Core\Cli\Color::MAGENTA });
// A file name a user typed. `Core\Str::fromCodePoint(27)` returns the escape// character. In a terminal it begins a command that clears the screen.string $file = 'may' . Core\Str::fromCodePoint(27) . '[2J.csv';
var $line = Core\Cli\Text::plain('reading ') + Core\Cli\Text::styled($file, $chosen);
// The escape character prints as the symbol `␛`, so this prints// "reading may␛[2J.csv".echo $line, "\n";reading may␛[2J.csvHighlighting the word a search found
<?nvs// Shows the everyday use: a search tool colours the word it found inside each// line it prints. This example does not run in a terminal, so the styling is// dropped and only the text prints.
var $hit = Core\Cli\Style::of({ color: Core\Cli\Color::YELLOW, bold: true });string $word = 'error';array<string> $lines = [ '09:15 error connecting to the database', '09:15 retrying in five seconds', '09:16 connected',];
foreach ($lines as string $line) { if (Core\Str::contains($line, $word)) { // The line is cut in three pieces: what comes before the word, the // word itself with the style on it, and what comes after. echo Core\Cli\Text::plain(Core\Str::before($line, $word) ?? '') + Core\Cli\Text::styled($word, $hit) + Core\Cli\Text::plain(Core\Str::after($line, $word) ?? ''), "\n"; } else { echo Core\Cli\Text::plain($line), "\n"; }}09:15 error connecting to the database09:15 retrying in five seconds09:16 connected