From 3cbfa63d051f0d5263b670184573d83ba4e6f538 Mon Sep 17 00:00:00 2001 From: Viacheslav Sychov Date: Thu, 8 Oct 2015 10:06:00 +0100 Subject: [PATCH 1/2] fix bug with set max count, by start method in progress bar --- .../Component/Console/Helper/ProgressBar.php | 38 +++++++++++++------ .../Console/Tests/Helper/ProgressBarTest.php | 21 ++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 7f4b2efb8d..0c0a3fc254 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -43,6 +43,7 @@ class ProgressBar private $formatLineCount; private $messages; private $overwrite = true; + private $formatSetByUser = false; private static $formatters; private static $formats; @@ -72,7 +73,7 @@ class ProgressBar } } - $this->setFormat($this->determineBestFormat()); + $this->setFormatInternal($this->determineBestFormat()); $this->startTime = time(); } @@ -310,16 +311,8 @@ class ProgressBar */ public function setFormat($format) { - // try to use the _nomax variant if available - if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) { - $this->format = self::getFormatDefinition($format.'_nomax'); - } elseif (null !== self::getFormatDefinition($format)) { - $this->format = self::getFormatDefinition($format); - } else { - $this->format = $format; - } - - $this->formatLineCount = substr_count($this->format, "\n"); + $this->formatSetByUser = true; + $this->setFormatInternal($format); } /** @@ -345,6 +338,10 @@ class ProgressBar if (null !== $max) { $this->setMaxSteps($max); + + if (!$this->formatSetByUser) { + $this->setFormatInternal($this->determineBestFormat()); + } } $this->display(); @@ -478,6 +475,25 @@ class ProgressBar $this->overwrite(str_repeat("\n", $this->formatLineCount)); } + /** + * Sets the progress bar format. + * + * @param string $format The format + */ + private function setFormatInternal($format) + { + // try to use the _nomax variant if available + if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) { + $this->format = self::getFormatDefinition($format.'_nomax'); + } elseif (null !== self::getFormatDefinition($format)) { + $this->format = self::getFormatDefinition($format); + } else { + $this->format = $format; + } + + $this->formatLineCount = substr_count($this->format, "\n"); + } + /** * Sets the progress bar maximal steps. * diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 78f537543e..dbcc6e2a8a 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -106,6 +106,27 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase ); } + public function testFormatWhenMaxInConstructAndInStart() + { + $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar->start(); + $bar->advance(10); + $bar->finish(); + + rewind($output->getStream()); + $maxInConstruct = stream_get_contents($output->getStream()); + + $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($maxInStart, $maxInConstruct); + } + public function testCustomizations() { $bar = new ProgressBar($output = $this->getOutputStream(), 10); From e651da4e6a5bbffb51ffb3e937d8e2e0ccf91c30 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 11 Oct 2015 09:29:49 +0200 Subject: [PATCH 2/2] [Console] fixed progress bar format on edge cases --- .../Component/Console/Helper/ProgressBar.php | 24 +++++++------ .../Console/Tests/Helper/ProgressBarTest.php | 34 ++++++++++++++++--- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 0c0a3fc254..0b64b18a1d 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -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')) { diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index dbcc6e2a8a..7d18c11847 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -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()