diff --git a/examples/common.php b/examples/common.php index b0ad234..5086a13 100644 --- a/examples/common.php +++ b/examples/common.php @@ -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(); +} diff --git a/examples/progress.php b/examples/progress.php index d79d126..6f05109 100644 --- a/examples/progress.php +++ b/examples/progress.php @@ -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); diff --git a/lib/cli/progress/Bar.php b/lib/cli/progress/Bar.php index e05ef50..3bb63fc 100644 --- a/lib/cli/progress/Bar.php +++ b/lib/cli/progress/Bar.php @@ -13,6 +13,7 @@ namespace cli\progress; use cli; +use cli\Notify; use cli\Progress; use cli\Shell; use cli\Streams; @@ -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); + } }