Skip to content

Core\Cli::showProgress

Draws a progress bar on the terminal while your code works.

Core\Cli::showProgress opens a bar scaled to $total and runs your code with a handle to it. Your code counts: advance says that one more unit of work is done, and the runtime draws the bar. { by: 1000 } counts a thousand units in one step, and { label: "..." } sets the caption beside the bar.

The bar closes when Core\Cli::showProgress returns, on every way out, and the terminal is left the way it was found. Where the output is not a terminal, a log file for example, nothing is drawn and your code still runs. A count past $total shows a full bar, so a loop that counts too far still draws a full bar. A $total of 0 is work already done, and draws a full bar.

Core\Cli::showProgress returns exactly what your code returned, at its own type. Core\Cli::showLive is the general form, for a region whose rows you write yourself.

The examples below count three files, say what is being done, and import rows in batches.

Parameters

  • $totaluintThe number of units of work for a full bar. A total of 0 means the work is already done, and it draws a full bar.
  • $bodycallable(Core\Cli\Progress): TThe work to do while the bar is open. Its return value is the return value of this method.

Return value

Returns T. Exactly what $body returned, with the same type, as for showLive.

Changelog

VersionChange
0.0.6The 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

Count the work as it is done

<?nvs
// Shows how to draw a progress bar while a program works. In a terminal you see
// one bar that fills up. This example does not run in a terminal, so the bar
// draws nothing and only the report at the end is printed.
uint $imported = Core\Cli::showProgress(3, fn(Core\Cli\Progress $bar): uint => {
uint $at = 0;
while ($at < 3) {
$at = $at + 1;
// One step of the three the bar was scaled to.
$bar->advance();
}
return $at;
});
// The bar is closed by now, so this is written under it. This prints 3.
echo 'imported ', $imported, " files\n";
Output
imported 3 files

Say what is being done

<?nvs
// Shows the caption beside the bar. A `label` says what the program is doing
// right now. Leave it out and the caption stays as it was.
string $last = Core\Cli::showProgress(3, fn(Core\Cli\Progress $bar): string => {
array<string> $steps = ['fetching', 'building', 'testing'];
string $current = '';
foreach ($steps as string $step) {
$current = $step;
$bar->advance({ label: $step });
}
return $current;
});
// `Core\Cli::showProgress` returns what the body returned. This prints "testing".
echo 'the last step was ', $last, "\n";
Output
the last step was testing

Import rows in batches

<?nvs
// Shows the everyday use: a program imports a large file in batches and counts
// the rows, not the batches. `by` says how many units one step finished, so the
// bar is scaled to the work a person cares about.
uint $rows = Core\Cli::showProgress(5000, fn(Core\Cli\Progress $bar): uint => {
uint $done = 0;
while ($done < 5000) {
// Each batch reads a thousand rows, so one step counts a thousand.
$done = $done + 1000;
$bar->advance({ by: 1000, label: Core\Str::format('%d of 5000 rows', $done) });
}
return $done;
});
echo 'imported ', $rows, " rows\n";
Output
imported 5000 rows