Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding optional msg parameter to \cli\Progress\Bar::tick() #126

Merged
merged 3 commits into from Mar 12, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/common.php
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
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
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);
}
}