Skip to content

Commit

Permalink
bug #19134 Distinguish between first and subsequent progress bar disp…
Browse files Browse the repository at this point in the history
…lays (rquadling)

This PR was merged into the 2.7 branch.

Discussion
----------

Distinguish between first and subsequent progress bar displays

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19133
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

Fixes #19133

When a progress bar is first displayed, if it is multi-line, previously output lines are erased, depending upon the number of lines in the progress bar.

This patch fixes that be distinguishing between the first display (no erasing of previous output) and subsequent displays of the progress bar.

Commits
-------

3871e1a Differentiate between the first time a progress bar is displayed and subsequent times
  • Loading branch information
fabpot committed Jun 23, 2016
2 parents b8c767e + 3871e1a commit 448a390
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 448a390

Please sign in to comment.