Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/progress-step-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

require_once 'common.php';

// Example 1: Default percentage-based format
echo "Example 1: Default percentage-based format\n";
$progress = new \cli\progress\Bar('Default format', 10);
for ($i = 0; $i < 10; $i++) {
$progress->tick();
usleep(100000);
}
$progress->finish();

echo "\n";

// Example 2: Step-based format (current/total)
echo "Example 2: Step-based format (current/total)\n";
$progress = new \cli\progress\Bar(
'Step format',
10,
100,
'{:msg} {:current}/{:total} [' // Custom formatMessage with current/total
);
for ($i = 0; $i < 10; $i++) {
$progress->tick();
usleep(100000);
}
$progress->finish();

echo "\n";

// Example 3: Custom format combining steps and percentage
echo "Example 3: Custom format combining steps and percentage\n";
$progress = new \cli\progress\Bar(
'Mixed format',
50,
100,
'{:msg} {:current}/{:total} ({:percent}%) [' // Both current/total and percent
);
for ($i = 0; $i < 50; $i++) {
$progress->tick();
usleep(50000);
}
$progress->finish();

echo "\n";

// Example 4: Large numbers with step format
echo "Example 4: Large numbers with step format\n";
$progress = new \cli\progress\Bar(
'Processing items',
1000,
100,
'{:msg} {:current}/{:total} ['
);
for ($i = 0; $i < 1000; $i += 50) {
$progress->tick(50);
usleep(20000);
}
$progress->finish();
32 changes: 29 additions & 3 deletions lib/cli/progress/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ class Bar extends Progress {
protected $_formatTiming = '] {:elapsed} / {:estimated}';
protected $_format = '{:msg}{:bar}{:timing}';

/**
* Instantiates a Progress Bar.
*
* @param string $msg The text to display next to the Notifier.
* @param int $total The total number of ticks we will be performing.
* @param int $interval The interval in milliseconds between updates.
* @param string|null $formatMessage Optional format string for the message portion.
* @param string|null $formatTiming Optional format string for the timing portion.
* @param string|null $format Optional format string for the overall display.
*/
public function __construct($msg, $total, $interval = 100, $formatMessage = null, $formatTiming = null, $format = null) {
parent::__construct($msg, $total, $interval);

if ($formatMessage !== null) {
$this->_formatMessage = $formatMessage;
}
if ($formatTiming !== null) {
$this->_formatTiming = $formatTiming;
}
if ($format !== null) {
$this->_format = $format;
}
}

/**
* Prints the progress bar to the screen with percent complete, elapsed time
* and estimated total time.
Expand All @@ -49,11 +73,13 @@ public function display($finish = false) {

$percent = str_pad(floor($_percent * 100), 3);
$msg = $this->_message;
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent'));
$current = $this->current();
$total = $this->total();
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent', 'current', 'total'));

$estimated = $this->formatTime($this->estimated());
$elapsed = str_pad($this->formatTime($this->elapsed()), strlen($estimated));
$timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated'));
$timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated', 'current', 'total', 'percent'));

$size = Shell::columns();
$size -= strlen($msg . $timing);
Expand All @@ -65,7 +91,7 @@ public function display($finish = false) {
// substr is needed to trim off the bar cap at 100%
$bar = substr(str_pad($bar, $size, ' '), 0, $size);

Streams::out($this->_format, compact('msg', 'bar', 'timing'));
Streams::out($this->_format, compact('msg', 'bar', 'timing', 'current', 'total', 'percent'));
}

/**
Expand Down