Skip to content

Commit

Permalink
[Console] added a way to add a custom message on a progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 1, 2014
1 parent 7a30e50 commit a9d47eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
60 changes: 41 additions & 19 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Expand Up @@ -23,11 +23,11 @@
class ProgressBar
{
const FORMAT_QUIET = ' %percent%%';
const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent%%';
const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent%% Elapsed: %elapsed%';
const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent:3s%%';
const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%';
const FORMAT_QUIET_NOMAX = ' %current%';
const FORMAT_NORMAL_NOMAX = ' %current% [%bar%]';
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed%';
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed:6s%';

// options
private $barWidth = 28;
Expand All @@ -49,6 +49,7 @@ class ProgressBar
private $lastMessagesLength;
private $barCharOriginal;
private $formatLineCount;
private $messages;

static private $formatters;

Expand Down Expand Up @@ -87,6 +88,16 @@ public static function setPlaceholderFormatter($name, $callable)
self::$formatters[$name] = $callable;
}

public function setMessage($message, $name = 'message')
{
$this->messages[$name] = $message;
}

public function getMessage($name = 'message')
{
return $this->messages[$name];
}

/**
* Gets the progress bar start time.
*
Expand Down Expand Up @@ -337,10 +348,21 @@ public function display()
throw new \LogicException('You must start the progress bar before calling display().');
}

$regex = implode('|', array_keys(self::$formatters));
$self = $this;
$this->overwrite(preg_replace_callback("{($regex)}", function ($matches) use ($self) {
return call_user_func(self::$formatters[$matches[1]], $self);
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
if (isset(self::$formatters[$matches[1]])) {
$text = call_user_func(self::$formatters[$matches[1]], $self);
} elseif (isset($this->messages[$matches[1]])) {
$text = $this->messages[$matches[1]];
} else {
return $matches[0];
}

if (isset($matches[2])) {
$text = sprintf('%'.$matches[2], $text);
}

return $text;
}, $this->format));
}

Expand Down Expand Up @@ -413,7 +435,7 @@ private function determineBestFormat()
static private function initPlaceholderFormatters()
{
return array(
'%bar%' => function (ProgressBar $bar) {
'bar' => function (ProgressBar $bar) {
$completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getStep() % $bar->getBarWidth());
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlen($bar->getProgressCharacter());
$display = str_repeat($bar->getBarCharacter(), $completeBars);
Expand All @@ -423,10 +445,10 @@ static private function initPlaceholderFormatters()

return $display;
},
'%elapsed%' => function (ProgressBar $bar) {
return str_pad(Helper::formatTime(time() - $bar->getStartTime()), 6, ' ', STR_PAD_LEFT);
'elapsed' => function (ProgressBar $bar) {
return Helper::formatTime(time() - $bar->getStartTime());
},
'%remaining%' => function (ProgressBar $bar) {
'remaining' => function (ProgressBar $bar) {
if (!$bar->getMaxSteps()) {
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
}
Expand All @@ -437,9 +459,9 @@ static private function initPlaceholderFormatters()
$remaining = round((time() - $bar->getStartTime()) / $bar->getStep() * ($bar->getMaxSteps() - $bar->getStep()));
}

return str_pad(Helper::formatTime($remaining), 6, ' ', STR_PAD_LEFT);
return Helper::formatTime($remaining);
},
'%estimated%' => function (ProgressBar $bar) {
'estimated' => function (ProgressBar $bar) {
if (!$bar->getMaxSteps()) {
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
}
Expand All @@ -450,19 +472,19 @@ static private function initPlaceholderFormatters()
$estimated = round((time() - $bar->getStartTime()) / $bar->getStep() * $bar->getMaxSteps());
}

return str_pad(Helper::formatTime($estimated), 6, ' ', STR_PAD_LEFT);
return Helper::formatTime($estimated);
},
'%memory%' => function (ProgressBar $bar) {
return str_pad(Helper::formatMemory(memory_get_usage(true)), 6, ' ', STR_PAD_LEFT);;
'memory' => function (ProgressBar $bar) {
return Helper::formatMemory(memory_get_usage(true));
},
'%current%' => function (ProgressBar $bar) {
'current' => function (ProgressBar $bar) {
return str_pad($bar->getStep(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
},
'%max%' => function (ProgressBar $bar) {
'max' => function (ProgressBar $bar) {
return $bar->getMaxSteps();
},
'%percent%' => function (ProgressBar $bar) {
return str_pad(floor($bar->getProgressPercent() * 100), 3, ' ', STR_PAD_LEFT);
'percent' => function (ProgressBar $bar) {
return floor($bar->getProgressPercent() * 100);
},
);
}
Expand Down
Expand Up @@ -69,7 +69,7 @@ public function testCustomizations()
$bar->setBarCharacter('_');
$bar->setEmptyBarCharacter(' ');
$bar->setProgressCharacter('/');
$bar->setFormat(' %current%/%max% [%bar%] %percent%%');
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
$bar->start();
$bar->advance();

Expand Down Expand Up @@ -102,7 +102,7 @@ public function testPercent()
public function testOverwriteWithShorterLine()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->setFormat(' %current%/%max% [%bar%] %percent%%');
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
$bar->start();
$bar->display();
$bar->advance();
Expand Down Expand Up @@ -300,7 +300,7 @@ public function testParallelBars()

public function testAddingPlaceholderFormatter()
{
ProgressBar::setPlaceholderFormatter('%remaining_steps%', function (ProgressBar $bar) {
ProgressBar::setPlaceholderFormatter('remaining_steps', function (ProgressBar $bar) {
return $bar->getMaxSteps() - $bar->getStep();
});
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
Expand Down

0 comments on commit a9d47eb

Please sign in to comment.