Core\Cli::write
Core\Cli::write(
string|Core\Cli\Text $value,
{stream?: Core\Cli::Stream, newline?: bool},
): voidWrites 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. Ataintedvalue is written like any other. Replacing the control bytes makes it safe for the terminal, so no other check is needed. Asecretvalue is a compile error.
Options
streamCore\Cli::StreamoptionalWhich standard stream to write to. The default isOut.Errwrites to this request's diagnostic output, whichCore\Out::capturedoes not capture.Inthrows an error.newlinebooloptionalWhether to add oneLFafter the value. The default isfalse, 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$streamisCore\Cli::Stream::In. A program reads standard input and never writes to it. The case exists forCore\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 });read check save doneSend 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 });2 files, 314 ordersPrint 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;}Ada 3注文 12Grete 7