Skip to content

Core\Cli\Text::plain

Makes terminal text out of a string, with every control character replaced by a visible symbol.

A terminal does not only show text. It also reads commands out of it: a few bytes can clear the screen, move the cursor, change the window title or hide what comes next. Any string your program did not write itself can contain them, such as a file name, a row from a database or something a user typed.

Core\Cli\Text::plain takes a string and gives you a Core\Cli\Text. Every control character in it is replaced by a symbol you can see, so an escape character arrives on screen as ␛ and is never run. What you get back is a value you can print with echo, join to other terminal text with +, use as a row of a live region, and read back as a string with text().

Core\Cli\Text::styled is the same thing with a colour. The two are the only way to make terminal text, and neither can produce a command the terminal runs.

The examples below print one piece of text, show what happens to an escape sequence, and build a line out of plain and coloured parts.

Parameters

  • $textstringThe text to convert. The result is not tainted, for the same reason as in Core\Cli::escape. Nothing is left in it that could control the terminal.

Return value

Returns Core\Cli\Text. A Core\Cli\Text with the safe text. You can join it with another Text, and echo writes it. It has no styling. Use styled for styling.

Examples

Text for the terminal

<?nvs
// Shows how to make terminal text out of a string and print it.
string $name = 'orders.csv';
var $line = Core\Cli\Text::plain(Core\Str::format('reading %s', $name));
// A `Core\Cli\Text` is printed with `echo`, like a string.
echo $line, "\n";
// `text()` gives the string back, without any styling. This prints
// "reading orders.csv".
echo $line->text(), "\n";
Output
reading orders.csv
reading orders.csv

Control characters become visible

<?nvs
// Shows what happens to a string that carries terminal commands. A file name
// from outside your program may contain them, and after `plain` they are
// symbols on the screen instead of commands the terminal runs.
// `Core\Str::fromCodePoint(27)` returns the escape character. In a terminal,
// `[2J` after it clears the screen, and `[31m` after it turns the text red.
string $esc = Core\Str::fromCodePoint(27);
string $fromOutside = "report{$esc}[2J{$esc}[31m.csv";
// This prints "report␛[2J␛[31m.csv". The screen is not cleared and the text
// does not turn red.
echo Core\Cli\Text::plain($fromOutside), "\n";
// A tab and a newline are not commands, so they are left as they are.
echo Core\Cli\Text::plain("first\tsecond\nthird"), "\n";
Output
report␛[2J␛[31m.csv
first second
third

A line of plain and coloured parts

<?nvs
// Shows the everyday use: a tool prints one line per file, where the state is
// coloured and the file name is not. `+` joins terminal text. This example does
// not run in a terminal, so the colours are 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'];
foreach ($files as string $file) {
// The state is written by your program, so it can be coloured. The file
// name comes from outside, so it goes through `plain`.
var $line = Core\Cli\Text::styled('failed', $failed)
+ Core\Cli\Text::plain(Core\Str::format(' %s', $file));
echo $line, "\n";
}
Output
failed invoices.csv
failed orders.csv