Navigation Menu

Skip to content

Commit

Permalink
* Added \cli\Notify::elapsed(), returns elapsed time in seconds
Browse files Browse the repository at this point in the history
 * Added `\cli\Notify::speed()`, returns the number of ticks per second
 * Added `\cli\Notify::formatTime($time)`, returns the time in seconds formatted as 'M:SS'
 * Added `\cli\Progress::estimated()`, returns the estimated total time in seconds
 * `\cli\Progress::percent()` no longer multiplies by 100
 * `\cli\Progress` has a much better display, filling the entire width of the screen
  • Loading branch information
jlogsdon committed Apr 1, 2010
1 parent e401758 commit 239493f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
7 changes: 4 additions & 3 deletions example.php
Expand Up @@ -45,10 +45,10 @@
array('Kirestin', 'Stephens', 'Fitchburg', 'AB'),
);

function test_notify(\cli\Notify $notify, $cycle = 100, $sleep = 10) {
function test_notify(\cli\Notify $notify, $cycle = 1000000, $sleep = null) {
for ($i = 0; $i <= $cycle; $i++) {
$notify->tick();
usleep($sleep);
if ($sleep) usleep($sleep);
}
$notify->finish();
}
Expand Down Expand Up @@ -84,7 +84,8 @@ function test_notify(\cli\Notify $notify, $cycle = 100, $sleep = 10) {
test_notify(new \cli\notify\Spinner(' \cli\notify\Spinner cycles through a set of characters to emulate a spinner'));
break;
case 'progress_bar':
test_notify(new \cli\progress\Bar(' \cli\progress\Bar displays a progress bar', 100));
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));
break;
case 'table':
$table = new \cli\Table();
Expand Down
34 changes: 33 additions & 1 deletion lib/cli/Notify.php
Expand Up @@ -7,6 +7,7 @@ abstract class Notify {
protected $_first = true;
protected $_interval;
protected $_message;
protected $_start;
protected $_timer;

public function __construct($msg, $interval = 100) {
Expand All @@ -20,6 +21,37 @@ public function current() {
return number_format($this->_current);
}

public function elapsed() {
$elapsed = time() - $this->_start;
return $elapsed;
}

public function speed() {
static $tick, $iteration = 0, $speed = 0;

if (!$this->_start) {
return 0;
} else if (!$tick) {
$tick = $this->_start;
}

$now = microtime(true);
$span = $now - $tick;
if ($span > 1) {
$iteration++;
$tick = $now;
$speed = ($this->_current / $iteration) / $span;
}

return $speed;
}

public function formatTime($time) {
$minutes = $time / 60;
$seconds = $time % 60;
return floor($time / 60).':'.str_pad($time % 60, 2, 0, STR_PAD_LEFT);
}

public function finish() {
\cli\out("\r");
$this->display(true);
Expand All @@ -38,7 +70,7 @@ public function shouldUpdate() {
$now = microtime(true) * 1000;

if (empty($this->_timer)) {
$this->_timer = $now;
$this->_start = (int)(($this->_timer = $now) / 1000);
return true;
}

Expand Down
12 changes: 11 additions & 1 deletion lib/cli/Progress.php
Expand Up @@ -19,6 +19,16 @@ public function current() {
return str_pad(number_format($this->_current), $size);
}

public function estimated() {
$speed = $this->speed();
if (!$this->elapsed()) {
return 0;
}

$estimated = round($this->_total / $speed);
return $estimated;
}

public function finish() {
$this->_current = $this->_total;
parent::finish();
Expand All @@ -39,7 +49,7 @@ public function increment($idx = null) {
}

public function percent() {
return floor(($this->_current / $this->_total) * 100);
return ($this->_current / $this->_total);
}

public function total() {
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/notify/Dots.php
Expand Up @@ -21,6 +21,10 @@ public function display($finish = false) {
} else {
$dots = str_repeat('.', $this->_iteration++ % $this->_dots);
}
\cli\out('%s%-'.$this->_dots.'s', $this->_message, $dots);

$speed = number_format(round($this->speed()));
$elapsed = $this->formatTime($this->elapsed());

\cli\out_padded('%s%-'.$this->_dots.'s (%s, %s/s)', $this->_message, $dots, $elapsed, $speed);
}
}
5 changes: 4 additions & 1 deletion lib/cli/notify/Spinner.php
Expand Up @@ -21,6 +21,9 @@ public function display($finish = false) {
break;
}

\cli\out('%s %s', $this->_message, $char);
$speed = number_format(round($this->speed()));
$elapsed = $this->formatTime($this->elapsed());

\cli\out_padded('%s %s (%s, %s/s)', $this->_message, $char, $elapsed, $speed);
}
}
19 changes: 15 additions & 4 deletions lib/cli/progress/Bar.php
Expand Up @@ -3,11 +3,22 @@
namespace cli\progress;

class Bar extends \cli\Progress {
//The title 100% [=====> ] 0:00 / 0:00
public function display($finish = false) {
$per = $this->percent();
$bar = str_repeat('-', $per);
$bar = str_pad($bar, 100, '=').'>';
$percent = $this->percent();
$message = sprintf('%s %-3s%% [', $this->_message, floor($percent * 100));

\cli\out('%s [%-3s%%] %s', $this->_message, $per, $bar);
$elapsed = $this->formatTime($this->elapsed());
$estimated = $this->formatTime($this->estimated());
$timing = sprintf('] %-'.strlen($estimated).'s / %s', $elapsed, $estimated);

$size = \cli\Shell::columns();
$size -= strlen($message);
$size -= strlen($timing);

$bar = str_repeat('=', floor($percent * $size)).'>';
$bar = substr(str_pad($bar, $size, ' '), 0, $size);

\cli\out('%s%s%s', $message, $bar, $timing);
}
}

0 comments on commit 239493f

Please sign in to comment.