Skip to content

Commit 81c0147

Browse files
CopilotswissspidyCopilot
authored
Add format customization and step-based progress display to Bar (#196)
* Initial plan * Add format parameters and current/total placeholders to Bar progress Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Fix trailing whitespace and add example file Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Update lib/cli/progress/Bar.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update examples/progress-step-format.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix loop bounds and add placeholders to all format strings Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3de9a76 commit 81c0147

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

examples/progress-step-format.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
require_once 'common.php';
4+
5+
// Example 1: Default percentage-based format
6+
echo "Example 1: Default percentage-based format\n";
7+
$progress = new \cli\progress\Bar('Default format', 10);
8+
for ($i = 0; $i < 10; $i++) {
9+
$progress->tick();
10+
usleep(100000);
11+
}
12+
$progress->finish();
13+
14+
echo "\n";
15+
16+
// Example 2: Step-based format (current/total)
17+
echo "Example 2: Step-based format (current/total)\n";
18+
$progress = new \cli\progress\Bar(
19+
'Step format',
20+
10,
21+
100,
22+
'{:msg} {:current}/{:total} [' // Custom formatMessage with current/total
23+
);
24+
for ($i = 0; $i < 10; $i++) {
25+
$progress->tick();
26+
usleep(100000);
27+
}
28+
$progress->finish();
29+
30+
echo "\n";
31+
32+
// Example 3: Custom format combining steps and percentage
33+
echo "Example 3: Custom format combining steps and percentage\n";
34+
$progress = new \cli\progress\Bar(
35+
'Mixed format',
36+
50,
37+
100,
38+
'{:msg} {:current}/{:total} ({:percent}%) [' // Both current/total and percent
39+
);
40+
for ($i = 0; $i < 50; $i++) {
41+
$progress->tick();
42+
usleep(50000);
43+
}
44+
$progress->finish();
45+
46+
echo "\n";
47+
48+
// Example 4: Large numbers with step format
49+
echo "Example 4: Large numbers with step format\n";
50+
$progress = new \cli\progress\Bar(
51+
'Processing items',
52+
1000,
53+
100,
54+
'{:msg} {:current}/{:total} ['
55+
);
56+
for ($i = 0; $i < 1000; $i += 50) {
57+
$progress->tick(50);
58+
usleep(20000);
59+
}
60+
$progress->finish();

lib/cli/progress/Bar.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ class Bar extends Progress {
3131
protected $_formatTiming = '] {:elapsed} / {:estimated}';
3232
protected $_format = '{:msg}{:bar}{:timing}';
3333

34+
/**
35+
* Instantiates a Progress Bar.
36+
*
37+
* @param string $msg The text to display next to the Notifier.
38+
* @param int $total The total number of ticks we will be performing.
39+
* @param int $interval The interval in milliseconds between updates.
40+
* @param string|null $formatMessage Optional format string for the message portion.
41+
* @param string|null $formatTiming Optional format string for the timing portion.
42+
* @param string|null $format Optional format string for the overall display.
43+
*/
44+
public function __construct($msg, $total, $interval = 100, $formatMessage = null, $formatTiming = null, $format = null) {
45+
parent::__construct($msg, $total, $interval);
46+
47+
if ($formatMessage !== null) {
48+
$this->_formatMessage = $formatMessage;
49+
}
50+
if ($formatTiming !== null) {
51+
$this->_formatTiming = $formatTiming;
52+
}
53+
if ($format !== null) {
54+
$this->_format = $format;
55+
}
56+
}
57+
3458
/**
3559
* Prints the progress bar to the screen with percent complete, elapsed time
3660
* and estimated total time.
@@ -49,11 +73,13 @@ public function display($finish = false) {
4973

5074
$percent = str_pad(floor($_percent * 100), 3);
5175
$msg = $this->_message;
52-
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent'));
76+
$current = $this->current();
77+
$total = $this->total();
78+
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent', 'current', 'total'));
5379

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

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

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

7197
/**

0 commit comments

Comments
 (0)