Skip to content

Commit e4cb9fd

Browse files
authored
Merge pull request #126 from lf-jeremy/progress-bar-tick-message
adding optional msg parameter to \cli\Progress\Bar::tick()
2 parents 69863a0 + da1cd8d commit e4cb9fd

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

examples/common.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ function test_notify(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
2323
}
2424
$notify->finish();
2525
}
26+
27+
function test_notify_msg(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
28+
$notify->display();
29+
for ($i = 0; $i < $cycle; $i++) {
30+
// Sleep before tick to simulate time-intensive work and give time
31+
// for the initial message to display before it is changed
32+
if ($sleep) usleep($sleep);
33+
$msg = sprintf(' Finished step %d', $i + 1);
34+
$notify->tick(1, $msg);
35+
}
36+
$notify->finish();
37+
}

examples/progress.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
test_notify(new \cli\progress\Bar(' \cli\progress\Bar displays a progress bar', 1000000));
66
test_notify(new \cli\progress\Bar(' It sizes itself dynamically', 1000000));
7+
test_notify_msg(new \cli\progress\Bar(' It can even change its message', 5), 5, 1000000);

lib/cli/progress/Bar.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace cli\progress;
1414

1515
use cli;
16+
use cli\Notify;
1617
use cli\Progress;
1718
use cli\Shell;
1819
use cli\Streams;
@@ -66,4 +67,19 @@ public function display($finish = false) {
6667

6768
Streams::out($this->_format, compact('msg', 'bar', 'timing'));
6869
}
70+
71+
/**
72+
* This method augments the base definition from cli\Notify to optionally
73+
* allow passing a new message.
74+
*
75+
* @param int $increment The amount to increment by.
76+
* @param string $msg The text to display next to the Notifier. (optional)
77+
* @see cli\Notify::tick()
78+
*/
79+
public function tick($increment = 1, $msg = null) {
80+
if ($msg) {
81+
$this->_message = $msg;
82+
}
83+
Notify::tick($increment);
84+
}
6985
}

0 commit comments

Comments
 (0)