Skip to content

Core\Cli\Text::text

$text->text(): string

Reads what a piece of terminal text says, as a plain string, with the styling left out.

A Core\Cli\Text is made of runs: a piece of text and the style that piece wears. text() gives you those pieces joined, as a string, and no colour and no attribute comes with them. A sum of several pieces reads back as their text in order, and an empty one reads back as "".

You want this whenever something other than the terminal needs the same line. A log file takes the string, a database column takes it, and Core\Cli::displayWidth takes it when you count the columns a line will fill.

Every control character was replaced by a symbol you can see when the text was made, so what you read back holds none. You can put it straight into Core\Cli\Text::plain or Core\Cli\Text::styled again.

The examples below read one line back, write the same line to the screen in colour and to a log without it, and line up a column of coloured states.

Return value

Returns string. The text alone, with no escape sequences from a Cli\Style. You can pass it straight back to plain or styled. Replacing the control bytes a second time does not change the text.

Examples

Reading a line back

<?nvs
// Shows how to read back what a piece of terminal text says. The styling is
// left out, so what you get is a plain string.
var $warn = Core\Cli\Style::of({ color: Core\Cli\Color::YELLOW, bold: true });
var $line = Core\Cli\Text::styled('disk almost full', $warn);
// This prints "disk almost full". The colour is not part of the string.
echo $line->text(), "\n";
// A sum of several pieces reads back as their text in order, so this prints
// "disk almost full: 2 GB left".
var $whole = $line + Core\Cli\Text::plain(': 2 GB left');
echo $whole->text(), "\n";
Output
disk almost full
disk almost full: 2 GB left

The same line on the screen and in a log

<?nvs
// Shows one line going to the screen with colour, and the same line going to a
// log, where colour has no meaning. This example does not run in a terminal,
// so the colour is dropped and only the text prints.
var $failed = Core\Cli\Style::of({ color: Core\Cli\Color::RED, bold: true });
array<string> $files = ['invoices.csv', 'orders.csv'];
string $log = '';
foreach ($files as string $file) {
var $line = Core\Cli\Text::styled('failed', $failed)
+ Core\Cli\Text::plain(Core\Str::format(' %s', $file));
echo $line, "\n";
// `text()` gives the string a log file can keep.
$log = Core\Str::format("%s%s\n", $log, $line->text());
}
echo "--- the log ---\n", $log;
Output
failed invoices.csv
failed orders.csv
--- the log ---
failed invoices.csv
failed orders.csv

Lining up a column of states

<?nvs
// Shows the everyday use: a tool lines up a column of coloured states. This
// example does not run in a terminal, so the colours are dropped and only the
// text prints.
var $green = Core\Cli\Style::of({ color: Core\Cli\Color::GREEN });
array<string> $states = ['ok', 'skipped', 'failed'];
array<string> $files = ['invoices.csv', 'orders.csv', 'returns.csv'];
uint $at = 0;
foreach ($states as string $state) {
var $cell = Core\Cli\Text::styled($state, $green);
// `Core\Cli::displayWidth` counts the columns a string fills on screen. It
// takes a string, so the width of the cell is counted on `text()`.
uint $spaces = 10 - Core\Cli::displayWidth($cell->text());
echo $cell + Core\Cli\Text::plain(Core\Str::repeat(' ', $spaces)),
$files[$at as int], "\n";
$at = $at + 1;
}
Output
ok invoices.csv
skipped orders.csv
failed returns.csv