diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 261141ff1d78..d3fda634f64e 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 = array(); private $overwrite = true; + private $firstRun = true; private static $formatters; private static $formats; @@ -522,20 +523,24 @@ private function setMaxSteps($max) private function overwrite($message) { if ($this->overwrite) { - // Move the cursor to the beginning of the line - $this->output->write("\x0D"); + if (!$this->isFirstRun()) { + // Move the cursor to the beginning of the line + $this->output->write("\x0D"); - // Erase the line - $this->output->write("\x1B[2K"); + // Erase the line + $this->output->write("\x1B[2K"); - // Erase previous lines - if ($this->formatLineCount > 0) { - $this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount)); + // Erase previous lines + if ($this->formatLineCount > 0) { + $this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount)); + } } } elseif ($this->step > 0) { $this->output->writeln(''); } + $this->setFirstRun(false); + $this->output->write($message); } @@ -627,4 +632,14 @@ private static function initFormats() 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%', ); } + + private function isFirstRun() + { + return $this->firstRun; + } + + private function setFirstRun($firstRun) + { + $this->firstRun = (bool) $firstRun; + } } diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 04f3dbe4a73c..ebbb1d1172c5 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -29,7 +29,7 @@ public function testMultipleStart() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'). + ' 0 [>---------------------------]'. $this->generateOutput(' 1 [->--------------------------]'). $this->generateOutput(' 0 [>---------------------------]'), stream_get_contents($output->getStream()) @@ -44,7 +44,7 @@ public function testAdvance() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'). + ' 0 [>---------------------------]'. $this->generateOutput(' 1 [->--------------------------]'), stream_get_contents($output->getStream()) ); @@ -58,7 +58,7 @@ public function testAdvanceWithStep() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'). + ' 0 [>---------------------------]'. $this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()) ); @@ -73,7 +73,7 @@ public function testAdvanceMultipleTimes() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'). + ' 0 [>---------------------------]'. $this->generateOutput(' 3 [--->------------------------]'). $this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()) @@ -89,7 +89,7 @@ public function testAdvanceOverMax() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 9/10 [=========================>--] 90%'). + ' 9/10 [=========================>--] 90%'. $this->generateOutput(' 10/10 [============================] 100%'). $this->generateOutput(' 11/11 [============================] 100%'), stream_get_contents($output->getStream()) @@ -99,7 +99,7 @@ public function testAdvanceOverMax() public function testFormat() { $expected = - $this->generateOutput(' 0/10 [>---------------------------] 0%'). + ' 0/10 [>---------------------------] 0%'. $this->generateOutput(' 10/10 [============================] 100%'). $this->generateOutput(' 10/10 [============================] 100%') ; @@ -156,7 +156,7 @@ public function testCustomizations() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/10 [/ ] 0%'). + ' 0/10 [/ ] 0%'. $this->generateOutput(' 1/10 [_/ ] 10%'), stream_get_contents($output->getStream()) ); @@ -169,7 +169,7 @@ public function testDisplayWithoutStart() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------] 0%'), + ' 0/50 [>---------------------------] 0%', stream_get_contents($output->getStream()) ); } @@ -193,7 +193,7 @@ public function testFinishWithoutStart() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 50/50 [============================] 100%'), + ' 50/50 [============================] 100%', stream_get_contents($output->getStream()) ); } @@ -208,7 +208,7 @@ public function testPercent() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------] 0%'). + ' 0/50 [>---------------------------] 0%'. $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 1/50 [>---------------------------] 2%'). $this->generateOutput(' 2/50 [=>--------------------------] 4%'), @@ -230,7 +230,7 @@ public function testOverwriteWithShorterLine() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------] 0%'). + ' 0/50 [>---------------------------] 0%'. $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 1/50 [>---------------------------] 2%'). $this->generateOutput(' 2/50 [=>--------------------------]'), @@ -247,7 +247,7 @@ public function testStartWithMax() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------]'). + ' 0/50 [>---------------------------]'. $this->generateOutput(' 1/50 [>---------------------------]'), stream_get_contents($output->getStream()) ); @@ -264,7 +264,7 @@ public function testSetCurrentProgress() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------] 0%'). + ' 0/50 [>---------------------------] 0%'. $this->generateOutput(' 0/50 [>---------------------------] 0%'). $this->generateOutput(' 1/50 [>---------------------------] 2%'). $this->generateOutput(' 15/50 [========>-------------------] 30%'). @@ -339,7 +339,7 @@ public function testMultiByteSupport() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'). + ' 0 [>---------------------------]'. $this->generateOutput(' 3 [■■■>------------------------]'), stream_get_contents($output->getStream()) ); @@ -354,7 +354,7 @@ public function testClear() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/50 [>---------------------------] 0%'). + ' 0/50 [>---------------------------] 0%'. $this->generateOutput(' 25/50 [==============>-------------] 50%'). $this->generateOutput(''), stream_get_contents($output->getStream()) @@ -371,7 +371,7 @@ public function testPercentNotHundredBeforeComplete() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/200 [>---------------------------] 0%'). + ' 0/200 [>---------------------------] 0%'. $this->generateOutput(' 0/200 [>---------------------------] 0%'). $this->generateOutput(' 199/200 [===========================>] 99%'). $this->generateOutput(' 200/200 [============================] 100%'), @@ -471,9 +471,9 @@ public function testParallelBars() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/2 [>---------------------------] 0%')."\n". - $this->generateOutput(' 0/3 [#---------------------------] 0%')."\n". - rtrim($this->generateOutput(' 0 [>---------------------------]')). + ' 0/2 [>---------------------------] 0%'."\n". + ' 0/3 [#---------------------------] 0%'."\n". + rtrim(' 0 [>---------------------------]'). "\033[2A". $this->generateOutput(' 1/2 [==============>-------------] 50%')."\n". @@ -511,7 +511,7 @@ public function testWithoutMax() rewind($output->getStream()); $this->assertEquals( - rtrim($this->generateOutput(' 0 [>---------------------------]')). + rtrim(' 0 [>---------------------------]'). rtrim($this->generateOutput(' 1 [->--------------------------]')). rtrim($this->generateOutput(' 2 [-->-------------------------]')). rtrim($this->generateOutput(' 3 [--->------------------------]')). @@ -534,7 +534,7 @@ public function testAddingPlaceholderFormatter() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 3 [>---------------------------]'). + ' 3 [>---------------------------]'. $this->generateOutput(' 2 [=========>------------------]'). $this->generateOutput(' 0 [============================]'), stream_get_contents($output->getStream()) @@ -553,7 +553,7 @@ public function testMultilineFormat() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(">---------------------------\nfoobar"). + ">---------------------------\nfoobar". $this->generateOutput("=========>------------------\nfoobar"). "\x0D\x1B[2K\x1B[1A\x1B[2K". $this->generateOutput("============================\nfoobar"), @@ -588,11 +588,11 @@ public function testAnsiColorsAndEmojis() rewind($output->getStream()); $this->assertEquals( - $this->generateOutput( + " \033[44;37m Starting the demo... fingers crossed \033[0m\n". ' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n". " \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m" - ). + . $this->generateOutput( " \033[44;37m Looks good to me... \033[0m\n". ' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n". @@ -614,7 +614,7 @@ public function testSetFormat() $bar->start(); rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0 [>---------------------------]'), + ' 0 [>---------------------------]', stream_get_contents($output->getStream()) ); @@ -623,7 +623,7 @@ public function testSetFormat() $bar->start(); rewind($output->getStream()); $this->assertEquals( - $this->generateOutput(' 0/10 [>---------------------------] 0%'), + ' 0/10 [>---------------------------] 0%', stream_get_contents($output->getStream()) ); }