Skip to content

Core\Cli\Live::set

Replaces every row a live region shows.

Core\Cli::showLive opens a region on the terminal. A region is a block of rows that is drawn again in place. showLive gives your code a Core\Cli\Live, and set is its only method. You pass the rows the region shows now, one Core\Cli\Text each. They replace the rows it showed before. The region does not grow, and the terminal does not scroll. With fewer rows, the region becomes smaller. With an empty array, it shows nothing.

A row is a Core\Cli\Text, which Core\Cli\Text::plain makes from a string and Core\Cli\Text::styled makes with a colour. A row wider than the terminal is cut off at the last column. It is not wrapped onto a second line, so the region keeps its shape. Where the output is not a terminal, such as a log file, set draws nothing at all and your code still runs.

The examples below replace one row with another, give a row a colour, and show a table of transfers that becomes smaller as they finish.

Parameters

  • $linesarray<Core\Cli\Text>The area's new rows, one Core\Cli\Text each. They replace the old rows. A row wider than the terminal is cut to the terminal width. It is not wrapped, because a wrapped row would stop the area from being redrawn.

Return value

Returns void. Nothing. The rows are drawn now, or together with the next update. Your program cannot see the difference. The last rows given are always drawn before the area closes.

Errors

  • LogicErrorThis area is not the active one on the terminal. It has closed, or an inner Core\Cli::showLive is open inside it. A Core\Cli\Live works only inside the call that made it. Using it outside that call is a bug in the program.

Examples

Every call replaces every row

<?nvs
// Shows that `set` replaces the rows of a region instead of adding to them. In
// a terminal you see three rows, then two, then one. This example does not run
// in a terminal, so the region draws nothing and only the report is printed.
uint $rows = Core\Cli::showLive(fn(Core\Cli\Live $screen): uint => {
// Three rows. The region is three lines tall.
$screen->set([
Core\Cli\Text::plain('orders waiting'),
Core\Cli\Text::plain('invoices waiting'),
Core\Cli\Text::plain('reports waiting'),
]);
// Two rows. The row for the finished job is gone, and the region is two
// lines tall.
$screen->set([
Core\Cli\Text::plain('invoices running'),
Core\Cli\Text::plain('reports waiting'),
]);
// One row. This is what the region shows when the call ends.
array<Core\Cli\Text> $last = [Core\Cli\Text::plain('reports running')];
$screen->set($last);
return Core\Arr::count($last);
});
// This prints 1.
echo 'the region ended with ', $rows, " row\n";
Output
the region ended with 1 row

A row can have a colour

<?nvs
// Shows rows with a colour. A row is a `Core\Cli\Text`, so `Core\Cli\Text::styled`
// makes one that a terminal shows in colour. Without a terminal the colour is
// left out and the words stay.
Core\Cli\Style $good = Core\Cli\Style::of({ color: Core\Cli\Color::GREEN });
Core\Cli\Style $busy = Core\Cli\Style::of({ color: Core\Cli\Color::YELLOW });
string $state = Core\Cli::showLive(fn(Core\Cli\Live $screen): string => {
$screen->set([
Core\Cli\Text::styled('database ready', $good),
Core\Cli\Text::styled('cache warming up', $busy),
]);
// Both rows are replaced by two green ones when the second service is
// ready.
$screen->set([
Core\Cli\Text::styled('database ready', $good),
Core\Cli\Text::styled('cache ready', $good),
]);
return 'ready';
});
echo 'everything is ', $state, "\n";
Output
everything is ready

A table that becomes smaller

<?nvs
// Shows the everyday use: a tool copies several files and keeps one row per
// file on the screen. A file that is done loses its row, so the table becomes
// smaller until nothing is left.
uint $copied = Core\Cli::showLive(fn(Core\Cli\Live $screen): uint => {
array<string> $files = ['photos.zip', 'notes.txt', 'backup.db'];
uint $done = 0;
while ($done < Core\Arr::count($files)) {
// One row per file that is still being copied. The files already
// copied are not in the list any more.
array<Core\Cli\Text> $rows = [];
foreach (Core\Arr::slice($files, $done as int) as string $file) {
$rows[] = Core\Cli\Text::plain(Core\Str::format('copying %s', $file));
}
$screen->set($rows);
$done = $done + 1;
}
// An empty list clears the region. The screen is free for the report.
array<Core\Cli\Text> $none = [];
$screen->set($none);
return $done;
});
echo 'copied ', $copied, " files\n";
Output
copied 3 files