Skip to content

Core\Cli\Progress::advance

$progress->advance({by?: uint, label?: string}): void$progress->advance(
    {by?: uint, label?: string},
): void

Counts work as done on an open progress bar.

Core\Cli::showProgress opens a bar on the terminal and gives your code a Core\Cli\Progress. advance is its only method. Each call counts more of the work as finished and draws the bar again. by says how many units this step finished, and is one when you leave it out. label sets the caption beside the bar, and leaving it out keeps the caption that is already there.

The bar is scaled to the total you gave Core\Cli::showProgress. A bar counted past that total shows as complete. The handle belongs to the call that made it. Use it after that call has returned, or while a second bar is open inside it, and advance throws a LogicError. Where the output is not a terminal, such as a log file, advance draws nothing and your program still runs.

The examples below count one step at a time, and count a whole block of rows in one step. The last one shows the name of each file a tool is uploading beside the bar.

Options

  • byuintoptionalHow many units this step finished. The default is 1.
  • labelstringoptionalThe caption next to the bar. Control bytes are replaced, as in every other terminal write. Without it, the caption does not change.

Return value

Returns void. Nothing. A count past the total shows a full bar. So a loop that counts too far draws a finished bar, not a broken one.

Errors

  • LogicErrorThis bar is not the active area on the terminal. It has closed, or an inner area is open inside it. Core\Cli\Live::set throws for the same reasons.

Examples

One step at a time

<?nvs
// Shows the simplest use: a program does its work step by step and tells the
// bar after each one. This example does not run in a terminal, so the bar
// draws nothing and only the report is printed.
uint $steps = Core\Cli::showProgress(3, fn(Core\Cli\Progress $bar): uint => {
array<string> $work = ['read the file', 'check the rows', 'write the report'];
uint $done = 0;
foreach ($work as string $step) {
// Your program does the step here. Then `advance` counts it. One call
// is one unit of work, because `by` is one when you leave it out.
$bar->advance();
$done = $done + 1;
}
return $done;
});
// This prints 3. The bar was full when the call ended.
echo 'finished ', $steps, " steps\n";
Output
finished 3 steps

A block of rows in one step

<?nvs
// Shows `by`, which counts more than one unit at a time. A file of 1000 rows is
// read in blocks of 250, so four steps fill the bar.
uint $rows = Core\Cli::showProgress(1000, fn(Core\Cli\Progress $bar): uint => {
uint $read = 0;
while ($read < 1000) {
// Your program reads 250 rows here. `by` says the step finished 250
// units, and `label` writes the count beside the bar.
$read = $read + 250;
$bar->advance({ by: 250, label: Core\Str::format('%d of 1000 rows', $read) });
}
return $read;
});
// This prints 1000.
echo 'read ', $rows, " rows\n";
Output
read 1000 rows

Naming the file being uploaded

<?nvs
// Shows the everyday use: a tool uploads several files and names the one it is
// working on beside the bar. The bar is scaled to the number of files, and each
// file that is sent moves it one step.
uint $sent = Core\Cli::showProgress(3, fn(Core\Cli\Progress $bar): uint => {
array<string> $files = ['photos.zip', 'notes.txt', 'backup.db'];
uint $done = 0;
foreach ($files as string $file) {
// Your program sends the file here. The caption says which one it is,
// and it stays beside the bar until the next call changes it.
$bar->advance({ label: Core\Str::format('sending %s', $file) });
$done = $done + 1;
}
// A step of `0` counts no more work. It only changes the caption, so the
// full bar ends by saying the upload is over.
$bar->advance({ by: 0, label: 'all files sent' });
return $done;
});
// This prints 3.
echo 'sent ', $sent, " files\n";
Output
sent 3 files