Progress bars
Progress bars
phpgt/cli supports dynamic, in-place progress updates for long-running tasks.
Use the ProgressBar helper
From inside a command, create a progress bar using createProgressBar():
$progressBar = $this->createProgressBar(100, "Installing", 30);
for($i = 0; $i <= 100; $i++) {
$progressBar->setProgress($i);
usleep(35_000);
}
$progressBar->finish("Installation complete");
This renders a single line that updates in place, similar to package manager output.
Progress bars write to StreamName::OUT by default. Pass a StreamName enum as the fourth argument to target a different stream:
$progressBar = $this->createProgressBar(
100,
"Installing",
30,
StreamName::ERROR
);
ProgressBar API
setProgress(int $current, ?string $label = null): voidSet an absolute progress value.advance(int $amount = 1, ?string $label = null): voidIncrement progress by a relative amount.finish(?string $label = null): voidComplete the bar and end the line.
Cursor management
Cursor movement is exposed through both Stream and command helper methods:
saveCursorPosition()restoreCursorPosition()moveCursorUp(int $amount = 1)moveCursorDown(int $amount = 1)moveCursorForward(int $amount = 1)moveCursorBack(int $amount = 1)setCursorColumn(int $column = 1)rewindCursor()clearLine()
These methods make it easy to build interactive terminal UIs beyond progress bars. Each cursor helper accepts an optional StreamName argument when cursor control should be written somewhere other than standard output.
See output streams for stream selection, or examples for runnable scripts including example/08-progress-bar.php.