[Console] fixed progress bar format on edge cases

This commit is contained in:
Fabien Potencier 2015-10-11 09:29:49 +02:00
parent 3cbfa63d05
commit e651da4e6a
2 changed files with 43 additions and 15 deletions

View File

@ -27,7 +27,8 @@ class ProgressBar
private $barChar; private $barChar;
private $emptyBarChar = '-'; private $emptyBarChar = '-';
private $progressChar = '>'; private $progressChar = '>';
private $format = null; private $format;
private $internalFormat;
private $redrawFreq = 1; private $redrawFreq = 1;
/** /**
@ -43,7 +44,6 @@ class ProgressBar
private $formatLineCount; private $formatLineCount;
private $messages; private $messages;
private $overwrite = true; private $overwrite = true;
private $formatSetByUser = false;
private static $formatters; private static $formatters;
private static $formats; private static $formats;
@ -73,8 +73,6 @@ class ProgressBar
} }
} }
$this->setFormatInternal($this->determineBestFormat());
$this->startTime = time(); $this->startTime = time();
} }
@ -311,8 +309,8 @@ class ProgressBar
*/ */
public function setFormat($format) public function setFormat($format)
{ {
$this->formatSetByUser = true; $this->format = null;
$this->setFormatInternal($format); $this->internalFormat = $format;
} }
/** /**
@ -338,10 +336,6 @@ class ProgressBar
if (null !== $max) { if (null !== $max) {
$this->setMaxSteps($max); $this->setMaxSteps($max);
if (!$this->formatSetByUser) {
$this->setFormatInternal($this->determineBestFormat());
}
} }
$this->display(); $this->display();
@ -438,6 +432,10 @@ class ProgressBar
return; return;
} }
if (null === $this->format) {
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped. // these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
$self = $this; $self = $this;
$output = $this->output; $output = $this->output;
@ -472,6 +470,10 @@ class ProgressBar
return; return;
} }
if (null === $this->format) {
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}
$this->overwrite(str_repeat("\n", $this->formatLineCount)); $this->overwrite(str_repeat("\n", $this->formatLineCount));
} }
@ -480,7 +482,7 @@ class ProgressBar
* *
* @param string $format The format * @param string $format The format
*/ */
private function setFormatInternal($format) private function setRealFormat($format)
{ {
// try to use the _nomax variant if available // try to use the _nomax variant if available
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) { if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {

View File

@ -106,25 +106,51 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testFormatWhenMaxInConstructAndInStart() public function testFormat()
{ {
$expected =
$this->generateOutput(' 0/10 [>---------------------------] 0%').
$this->generateOutput(' 10/10 [============================] 100%').
$this->generateOutput(' 10/10 [============================] 100%')
;
// max in construct, no format
$bar = new ProgressBar($output = $this->getOutputStream(), 10); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->start(); $bar->start();
$bar->advance(10); $bar->advance(10);
$bar->finish(); $bar->finish();
rewind($output->getStream()); rewind($output->getStream());
$maxInConstruct = stream_get_contents($output->getStream()); $this->assertEquals($expected, stream_get_contents($output->getStream()));
// max in start, no format
$bar = new ProgressBar($output = $this->getOutputStream()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(10); $bar->start(10);
$bar->advance(10); $bar->advance(10);
$bar->finish(); $bar->finish();
rewind($output->getStream()); rewind($output->getStream());
$maxInStart = stream_get_contents($output->getStream()); $this->assertEquals($expected, stream_get_contents($output->getStream()));
$this->assertEquals($maxInStart, $maxInConstruct); // max in construct, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setFormat('normal');
$bar->start();
$bar->advance(10);
$bar->finish();
rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));
// max in start, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('normal');
$bar->start(10);
$bar->advance(10);
$bar->finish();
rewind($output->getStream());
$this->assertEquals($expected, stream_get_contents($output->getStream()));
} }
public function testCustomizations() public function testCustomizations()