Core\Cli::showLive
Core\Cli::showLive(callable(Core\Cli\Live): T $body): TCore\Cli::showLive( callable(Core\Cli\Live): T $body, ): T
Keeps a block of lines on the screen up to date, in place.
A program that reports its progress usually needs one line that changes, not a hundred lines that scroll past. Core\Cli::showLive opens a region on the terminal and runs your code with a handle to it. set gives the region its rows, and every call replaces all of them. The region is drawn again each time. A row is a Core\Cli\Text, which Core\Cli\Text::plain makes from a string.
The region closes when Core\Cli::showLive returns, and it closes on every way out: a return, an error, or the program stopping. The terminal is left the way it was found. Where the output is not a terminal, a log file for example, the region draws nothing at all and your code still runs.
Core\Cli::showLive returns exactly what your code returned, at its own type.
The examples below keep one line up to date, show several rows at once, and work through a list of jobs.
Parameters
$bodycallable(Core\Cli\Live): TThe work to do while the area is open. Its return value is the return value of this method.
Return value
Returns T. Exactly what $body returned, with the same type. The terminal is restored however $body ends: a return, a throw or a fatal error. If the output is not a terminal, nothing is drawn, so no escape sequences are written.
Changelog
| Version | Change |
|---|---|
0.0.6 | The Core\Cli methods that prompt or draw now say so: askSecret, showLive, showProgress and selectMany. The old names were secret, live, progress and multiSelect. |
Examples
One line that keeps changing
<?nvs// Shows how to keep one line on the screen up to date. In a terminal you see a// single line that counts up. This example does not run in a terminal, so the// region draws nothing and only the report at the end is printed.
uint $checked = Core\Cli::showLive(fn(Core\Cli\Live $screen): uint => { uint $at = 0; while ($at < 3) { $at = $at + 1; // `set` replaces what the region shows. It does not add a line. $screen->set([Core\Cli\Text::plain(Core\Str::format('checking file %d of 3', $at))]); } return $at;});
// The region is closed by now, so this is written under it. This prints 3.echo 'checked ', $checked, " files\n";checked 3 filesSeveral rows at once
<?nvs// Shows a region of more than one row. Every call to `set` gives the whole// picture again, so all the rows change together.
string $last = Core\Cli::showLive(fn(Core\Cli\Live $screen): string => { array<string> $stages = ['fetching', 'building', 'testing']; string $current = ''; foreach ($stages as string $stage) { $current = $stage; $screen->set([ Core\Cli\Text::plain('build 42'), Core\Cli\Text::plain(Core\Str::format(' %s ...', $stage)), ]); } return $current;});
// `Core\Cli::showLive` returns what the body returned, at the body's own type. This// prints "testing".echo 'the last stage was ', $last, "\n";the last stage was testingWork through a list of jobs
<?nvs// Shows the everyday use: a script works through a list of jobs and keeps the// screen showing where it is. The region closes when `Core\Cli::showLive` returns, so// the report is written under it and stays on the screen.
array<string> $failed = Core\Cli::showLive(fn(Core\Cli\Live $screen): array<string> => { array<string> $jobs = ['orders', 'invoices', 'reports']; array<string> $bad = []; uint $at = 0; foreach ($jobs as string $job) { $at = $at + 1; $screen->set([ Core\Cli\Text::plain(Core\Str::format('job %d of 3: %s', $at, $job)), ]); if ($job == 'invoices') { $bad[] = $job; } } return $bad;});
// This prints 2 and then "invoices".echo 'finished ', 3 - Core\Arr::count($failed), " of 3 jobs\n";echo 'failed: ', Core\Str::join($failed, ', '), "\n";finished 2 of 3 jobsfailed: invoices