8000 [Console] A better progress bar by fabpot · Pull Request #10356 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] A better progress bar #10356

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

Merged
merged 9 commits into from
Mar 3, 2014
Prev Previous commit
Next Next commit
[Console] added a way to globally add a progress bar format or modify…
… a built-in one
  • Loading branch information
fabpot committed Mar 1, 2014
commit 244d3b81be40ebdd83f70f9b9a6587605f5b9355
67 changes: 40 additions & 27 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
*/
class ProgressBar
{
const FORMAT_QUIET = ' %percent%%';
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:6s%';

// options
private $barWidth = 28;
private $barChar = '=';
Expand All @@ -52,6 +45,7 @@ class ProgressBar
private $messages;

static private $formatters;
static private $formats;

/**
* Constructor.
Expand All @@ -69,6 +63,10 @@ public function __construct(OutputInterface $output, $max = 0)
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
}

if (!self::$formats) {
self::$formats = self::initFormats();
}
}

/**
Expand All @@ -79,7 +77,7 @@ public function __construct(OutputInterface $output, $max = 0)
* @param string $name The placeholder name (including the delimiter char like %)
* @param callable $callable A PHP callable
*/
public static function setPlaceholderFormatter($name, $callable)
public static function setPlaceholderFormatterDefinition($name, $callable)
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
Expand All @@ -88,6 +86,23 @@ public static function setPlaceholderFormatter($name, $callable)
self::$formatters[$name] = $callable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method initPlaceholderFormatters should be called here if the class has not been instantiated yet.

Use case:

ProgressBar::setPlaceholderFormatter('%cpu%', function (ProgressBar $bar) {});
$bar = new ProgressBar($output, 100);

}

/**
* Sets a format for a given name.
*
* This method also allow you to override an existing format.
*
* @param string $name The format name
* @param string $format A format string
*/
public static function setFormatDefinition($name, $format)
{
if (!self::$formats) {
self::$formats = self::initFormats();
}

self::$formats[$name] = $format;
}

public function setMessage($message, $name = 'message')
{
$this->messages[$name] = $message;
Expand Down Expand Up @@ -235,8 +250,8 @@ public function getProgressCharacter()
*/
public function setFormat($format)
{
$this->format = $format;
$this->formatLineCount = substr_count($format, "\n");
$this->format = isset(self::$formats[$format]) ? self::$formats[$format] : $format;
$this->formatLineCount = substr_count($this->format, "\n");
}

/**
Expand Down Expand Up @@ -408,28 +423,14 @@ private function determineBestFormat()
{
switch ($this->output->getVerbosity()) {
case OutputInterface::VERBOSITY_QUIET:
$format = self::FORMAT_QUIET_NOMAX;
if ($this->max > 0) {
$format = self::FORMAT_QUIET;
}
break;
return $this->max > 0 ? 'quiet' : 'quiet_nomax';
case OutputInterface::VERBOSITY_VERBOSE:
case OutputInterface::VERBOSITY_VERY_VERBOSE:
case OutputInterface::VERBOSITY_DEBUG:
$format = self::FORMAT_VERBOSE_NOMAX;
if ($this->max > 0) {
$format = self::FORMAT_VERBOSE;
}
break;
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
default:
$format = self::FORMAT_NORMAL_NOMAX;
if ($this->max > 0) {
$format = self::FORMAT_NORMAL;
}
break;
return $this->max > 0 ? 'normal' : 'normal_nomax';
}

return $format;
}

static private function initPlaceholderFormatters()
Expand Down Expand Up @@ -488,4 +489,16 @@ static private function initPlaceholderFormatters()
},
);
}

static private function initFormats()
{
return array(
'quiet' => ' %percent%%',
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%',
'quiet_nomax' => ' %current%',
'normal_nomax' => ' %current% [%bar%]',
'verbose_nomax' => ' %current% [%bar%] Elapsed: %elapsed:6s%',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function testParallelBars()

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