Skip to content

Core\Cli::write

Core\Cli::write(
    string|Core\Cli\Text $value,
    {stream?: Core\Cli::Stream, newline?: bool},
): void

Writes text to one of the terminal's standard streams.

Every program has two output streams. Standard output carries the result. Standard error carries the messages meant for the person running the program. Core\Cli::write writes to either one, so a result you send to a file stays clean while the progress notes still reach the screen.

Core\Cli::write writes exactly the characters you give it. It adds no newline. Ask for one with { newline: true }, and build a line from as many calls as you like.

Text that can control the terminal is made safe first. A control character in a string is replaced by a visible character that does nothing, the same replacement Core\Cli::escape makes. A Core\Cli\Text is written as it is, because its styling is there on purpose.

Good to know: standard input is not a stream a program writes to. Core\Cli::Stream::In throws an error.

The examples below build a line from pieces, send progress to standard error, and print a report whose columns line up.

Parameters

  • $valuestring|Core\Cli\TextThe text to write. A tainted value is written like any other. Replacing the control bytes makes it safe for the terminal, so no other check is needed. A secret value is a compile error.

Options

  • streamCore\Cli::StreamoptionalWhich standard stream to write to. The default is Out. Err writes to this request's diagnostic output, which Core\Out::capture does not capture. In throws an error.
  • newlinebooloptionalWhether to add one LF after the value. The default is false, so the method writes exactly the value it was given.

Return value

Returns void. Nothing. The bytes are written to the stream. If the write fails, the request ends.

Errors

  • LogicError$stream is Core\Cli::Stream::In. A program reads standard input and never writes to it. The case exists for Core\Cli::isTty, which can check all three streams.

Examples

Build a line from pieces

<?nvs
// Shows how to write text to the terminal. `Core\Cli::write` writes exactly the
// characters you give it, so several calls build one line.
array<string> $steps = ['read', 'check', 'save'];
// No newline is added here, so these calls stay on one line.
foreach ($steps as string $step) {
Core\Cli::write($step);
Core\Cli::write(' ');
}
// `{ newline: true }` adds one newline at the end. The line now reads
// "read check save done".
Core\Cli::write('done', { newline: true });
Output
read check save done

Send progress to standard error

<?nvs
// Shows how to keep the result apart from the progress notes. Standard output
// carries the result. Standard error carries what the person running the
// program should read.
array<string> $files = ['orders.csv', 'returns.csv'];
foreach ($files as string $file) {
// This message goes to standard error. It shows on the screen, and it is
// not part of the result when you send the output to a file.
Core\Cli::write('reading ' . $file, { stream: Core\Cli::Stream::Err, newline: true });
}
// This goes to standard output, which is the stream `Core\Cli::write` uses when
// you name none.
Core\Cli::write('2 files, 314 orders', { newline: true });
Output
2 files, 314 orders

Print a report whose columns line up

<?nvs
// Shows the everyday use: a program prints a report and the columns line up.
// `Core\Cli::write` adds nothing of its own, so each row is built piece by
// piece.
array<string> $names = ['Ada', '注文', 'Grete'];
array<string> $totals = ['3', '12', '7'];
int $row = 0;
while ($row < 3) {
string $name = $names[$row];
// A Japanese character takes two columns on screen.
// `Core\Cli::displayWidth` counts columns, so each name reaches column 8.
uint $pad = 8 - Core\Cli::displayWidth($name);
Core\Cli::write($name);
Core\Cli::write(Core\Str::repeat(' ', $pad));
Core\Cli::write($totals[$row], { newline: true });
$row = $row + 1;
}
Output
Ada 3
注文 12
Grete 7