Skip to content

Core\Cli::escape

Returns the text with every control character replaced by a visible one that does nothing.

A control character is not a letter. It is an order to the terminal: move the cursor, clear the screen, change the colour, rename the window. Text that came from a person or from a file can carry such orders. Core\Cli::escape replaces each one with a single character that stands for it and is safe to show. The escape character becomes ␛, a carriage return becomes ␍, the delete character becomes ␡. A tab and a newline lay text out rather than give orders, so they pass through.

echo already does this at the terminal. You need Core\Cli::escape when your program wants that safe text as a value: to keep it, to measure it, or to put it in a report.

Good to know: the result is a plain string. Tainted text comes back untainted, because nothing is left in it for a terminal to act on.

The examples below show an escape sequence as text, show what passes through, and hold what a person typed.

Parameters

  • $textstringThe text to make safe. The result is not tainted, because nothing is left in it that could control the terminal.

Return value

Returns string. The same text with these replacements, and no other change. This is not an HTML escaper, so <, & and " stay unchanged.

Examples

Show an escape sequence as text

<?nvs
// Shows how to turn text that could control the terminal into text that is safe
// to show. `Core\Cli::escape` replaces every control character with one visible
// character that does nothing.
// `Core\Str::fromCodePoint(27)` returns the escape character. A terminal reads
// this text as a command: print the rest in red.
string $colored = Core\Str::fromCodePoint(27) . '[31mred';
// This prints ␛[31mred. The escape character is now a picture of itself, and the
// terminal shows all eight characters.
echo Core\Cli::escape($colored), "\n";
Output
␛[31mred

A tab and a newline pass through

<?nvs
// Shows which characters `Core\Cli::escape` keeps. A tab and a newline lay text
// out, so they pass through and still work.
echo Core\Cli::escape("name\tprice\nheadphones\t49\n");
// A carriage return moves the cursor back over what is already on the line, so
// it is replaced by ␍. This prints one line: total: 49␍DONE
echo Core\Cli::escape("total: 49\rDONE"), "\n";
Output
name price
headphones 49
total: 49␍DONE

Hold what a person typed

<?nvs
// Shows the everyday use: a program keeps what a person typed and shows it
// later. `echo` already makes text safe for the terminal. `Core\Cli::escape`
// gives you that safe text as a value, so your program can hold it and measure
// it.
// There is no terminal here, so `ask` returns the default. The default ends with
// a control sequence that clears the screen.
string $clear = Core\Str::fromCodePoint(27) . '[2J';
tainted string $typed = Core\Cli::ask('Who is the order for?', { default: 'ada' . $clear });
// The result is a plain `string`, not a tainted one, and nothing in it can
// control a terminal.
string $safe = Core\Cli::escape($typed);
echo 'order for ', $safe, "\n";
echo 'the name takes ', Core\Cli::displayWidth($safe), " columns\n";
Output
order for ada␛[2J
the name takes 7 columns