[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 $emptyBarChar = '-';
private $progressChar = '>';
private $format = null;
private $format;
private $internalFormat;
private $redrawFreq = 1;
/**
@ -43,7 +44,6 @@ class ProgressBar
private $formatLineCount;
private $messages;
private $overwrite = true;
private $formatSetByUser = false;
private static $formatters;
private static $formats;
@ -73,8 +73,6 @@ class ProgressBar
}
}
$this->setFormatInternal($this->determineBestFormat());
$this->startTime = time();
}
@ -311,8 +309,8 @@ class ProgressBar
*/
public function setFormat($format)
{
$this->formatSetByUser = true;
$this->setFormatInternal($format);
$this->format = null;
$this->internalFormat = $format;
}
/**
@ -338,10 +336,6 @@ class ProgressBar
if (null !== $max) {
$this->setMaxSteps($max);
if (!$this->formatSetByUser) {
$this->setFormatInternal($this->determineBestFormat());
}
}
$this->display();
@ -438,6 +432,10 @@ class ProgressBar
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.
$self = $this;
$output = $this->output;
@ -472,6 +470,10 @@ class ProgressBar
return;
}
if (null === $this->format) {
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}
$this->overwrite(str_repeat("\n", $this->formatLineCount));
}
@ -480,7 +482,7 @@ class ProgressBar
*
* @param string $format The format
*/
private function setFormatInternal($format)
private function setRealFormat($format)
{
// try to use the _nomax variant if available
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->start();
$bar->advance(10);
$bar->finish();
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->start(10);
$bar->advance(10);
$bar->finish();
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()