Skip to content

Commit

Permalink
[Console] made formats even more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 3, 2014
1 parent 8c0022b commit 0d1a58c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
40 changes: 26 additions & 14 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
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
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 comments on commit 0d1a58c

Please sign in to comment.