Skip to content

Commit

Permalink
Differentiate between the first time a progress bar is displayed and …
Browse files Browse the repository at this point in the history
…subsequent times
  • Loading branch information
rquadling committed Jun 22, 2016
1 parent ec19a52 commit 3871e1a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
29 changes: 22 additions & 7 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Expand Up @@ -43,6 +43,7 @@ class ProgressBar
private $formatLineCount;
private $messages = array();
private $overwrite = true;
private $firstRun = true;

private static $formatters;
private static $formats;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
52 changes: 26 additions & 26 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Expand Up @@ -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())
Expand All @@ -44,7 +44,7 @@ public function testAdvance()

rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---------------------------]').
' 0 [>---------------------------]'.
$this->generateOutput(' 1 [->--------------------------]'),
stream_get_contents($output->getStream())
);
Expand All @@ -58,7 +58,7 @@ public function testAdvanceWithStep()

rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---------------------------]').
' 0 [>---------------------------]'.
$this->generateOutput(' 5 [----->----------------------]'),
stream_get_contents($output->getStream())
);
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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%')
;
Expand Down Expand Up @@ -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())
);
Expand All @@ -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())
);
}
Expand All @@ -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())
);
}
Expand All @@ -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%'),
Expand All @@ -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 [=>--------------------------]'),
Expand All @@ -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())
);
Expand All @@ -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%').
Expand Down Expand Up @@ -339,7 +339,7 @@ public function testMultiByteSupport()

rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---------------------------]').
' 0 [>---------------------------]'.
$this->generateOutput(' 3 [■■■>------------------------]'),
stream_get_contents($output->getStream())
);
Expand All @@ -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())
Expand All @@ -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%'),
Expand Down Expand Up @@ -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".
Expand Down Expand Up @@ -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 [--->------------------------]')).
Expand All @@ -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())
Expand All @@ -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"),
Expand Down Expand Up @@ -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".
Expand All @@ -614,7 +614,7 @@ public function testSetFormat()
$bar->start();
rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---------------------------]'),
' 0 [>---------------------------]',
stream_get_contents($output->getStream())
);

Expand All @@ -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())
);
}
Expand Down

0 comments on commit 3871e1a

Please sign in to comment.