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
[Console] made formats even more flexible
  • Loading branch information
fabpot committed Mar 3, 2014
commit 0d1a58c4698ede3543ff755ad63aa687b101d03f
40 changes: 26 additions & 14 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function __construct(OutputInterface $output, $max = 0)
if (!self::$formats) {
self::$formats = self::initFormats();
}

$this->setFormat($this->determineBestFormat());
}

/**
Expand Down Expand Up @@ -274,7 +276,15 @@ public function getProgressCharacter()
*/
public function setFormat($format)
{
$this->format = isset(self::$formats[$format]) ? self::$formats[$format] : $format;
// try to use the _nomax variant if available
if (!$this->max && isset(self::$formats[$format.'_nomax'])) {
$this->format = self::$formats[$format.'_nomax'];
} elseif (isset(self::$formats[$format])) {
$this->format = self::$formats[$format];
} else {
$this->format = $format;
}

$this->formatLineCount = substr_count($this->format, "\n");
}

Expand All @@ -299,10 +309,6 @@ public function start()
$this->lastMessagesLength = 0;
$this->barCharOriginal = '';

if (null === $this->format) {
$this->setFormat($this->determineBestFormat());
}

if (!$this->max) {
$this->barCharOriginal = $this->barChar;
$this->barChar = $this->emptyBarChar;
Expand Down Expand Up @@ -457,12 +463,13 @@ private function overwrite($message)
private function determineBestFormat()
{
switch ($this->output->getVerbosity()) {
case OutputInterface::VERBOSITY_QUIET:
return $this->max > 0 ? 'quiet' : 'quiet_nomax';
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
case OutputInterface::VERBOSITY_VERBOSE:
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
case OutputInterface::VERBOSITY_VERY_VERBOSE:
return $this->max > 0 ? 'very_verbose' : 'very_verbose_nomax';
case OutputInterface::VERBOSITY_DEBUG:
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
return $this->max > 0 ? 'debug' : 'debug_nomax';
default:
return $this->max > 0 ? 'normal' : 'normal_nomax';
}
Expand Down Expand Up @@ -528,12 +535,17 @@ private static function initPlaceholderFormatters()
private static 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%',
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
'normal_nomax' => ' %current% [%bar%]',

'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
'verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',

'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
'very_verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',

'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
'debug_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
);
}
}
22 changes: 21 additions & 1 deletion src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ public function testAnsiColorsAndEmojis()
$bar->finish();

rewind($output->getStream());

$this->assertEquals(
$this->generateOutput(
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
Expand All @@ -384,6 +383,27 @@ public function testAnsiColorsAndEmojis()
);
}

public function testSetFormat()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('normal');
$bar->start();
rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---------------------------]'),
stream_get_contents($output->getStream())
);

$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setFormat('normal');
$bar->start();
rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
stream_get_contents($output->getStream())
);
}

protected function getOutputStream($decorated = true)
{
return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);
Expand Down
0