Skip to content
Merged
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
12 changes: 12 additions & 0 deletions examples/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ function test_notify(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
}
$notify->finish();
}

function test_notify_msg(cli\Notify $notify, $cycle = 1000000, $sleep = null) {
$notify->display();
for ($i = 0; $i < $cycle; $i++) {
// Sleep before tick to simulate time-intensive work and give time
// for the initial message to display before it is changed
if ($sleep) usleep($sleep);
$msg = sprintf(' Finished step %d', $i + 1);
$notify->tick(1, $msg);
}
$notify->finish();
}
1 change: 1 addition & 0 deletions examples/progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

test_notify(new \cli\progress\Bar(' \cli\progress\Bar displays a progress bar', 1000000));
test_notify(new \cli\progress\Bar(' It sizes itself dynamically', 1000000));
test_notify_msg(new \cli\progress\Bar(' It can even change its message', 5), 5, 1000000);
16 changes: 16 additions & 0 deletions lib/cli/progress/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace cli\progress;

use cli;
use cli\Notify;
use cli\Progress;
use cli\Shell;
use cli\Streams;
Expand Down Expand Up @@ -66,4 +67,19 @@ public function display($finish = false) {

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

/**
* This method augments the base definition from cli\Notify to optionally
* allow passing a new message.
*
* @param int $increment The amount to increment by.
* @param string $msg The text to display next to the Notifier. (optional)
* @see cli\Notify::tick()
*/
public function tick($increment = 1, $msg = null) {
if ($msg) {
$this->_message = $msg;
}
Notify::tick($increment);
}
}