Skip to content

Commit

Permalink
Prevent ProgressBar redraw when message is same
Browse files Browse the repository at this point in the history
  • Loading branch information
fmasa authored and chalasr committed Oct 7, 2019
1 parent b43f255 commit 78b515f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class ProgressBar
private $messages = [];
private $overwrite = true;
private $terminal;
private $firstRun = true;
private $previousMessage;

private static $formatters;
private static $formats;
Expand Down Expand Up @@ -432,8 +432,14 @@ private function setRealFormat(string $format)
*/
private function overwrite(string $message): void
{
if ($this->previousMessage === $message) {
return;
}

$originalMessage = $message;

if ($this->overwrite) {
if (!$this->firstRun) {
if (null !== $this->previousMessage) {
if ($this->output instanceof ConsoleSectionOutput) {
$lines = floor(Helper::strlen($message) / $this->terminal->getWidth()) + $this->formatLineCount + 1;
$this->output->clear($lines);
Expand All @@ -451,7 +457,7 @@ private function overwrite(string $message): void
$message = PHP_EOL.$message;
}

$this->firstRun = false;
$this->previousMessage = $originalMessage;
$this->lastWriteTime = microtime(true);

$this->output->write($message);
Expand Down
21 changes: 14 additions & 7 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ public function testFormat()
{
$expected =
' 0/10 [>---------------------------] 0%'.
$this->generateOutput(' 10/10 [============================] 100%').
$this->generateOutput(' 10/10 [============================] 100%')
;

Expand Down Expand Up @@ -296,7 +295,6 @@ public function testPercent()
rewind($output->getStream());
$this->assertEquals(
' 0/50 [>---------------------------] 0%'.
$this->generateOutput(' 0/50 [>---------------------------] 0%').
$this->generateOutput(' 1/50 [>---------------------------] 2%').
$this->generateOutput(' 2/50 [=>--------------------------] 4%'),
stream_get_contents($output->getStream())
Expand All @@ -318,7 +316,6 @@ public function testOverwriteWithShorterLine()
rewind($output->getStream());
$this->assertEquals(
' 0/50 [>---------------------------] 0%'.
$this->generateOutput(' 0/50 [>---------------------------] 0%').
$this->generateOutput(' 1/50 [>---------------------------] 2%').
$this->generateOutput(' 2/50 [=>--------------------------]'),
stream_get_contents($output->getStream())
Expand All @@ -340,7 +337,6 @@ public function testOverwriteWithSectionOutput()
rewind($output->getStream());
$this->assertEquals(
' 0/50 [>---------------------------] 0%'.PHP_EOL.
"\x1b[1A\x1b[0J".' 0/50 [>---------------------------] 0%'.PHP_EOL.
"\x1b[1A\x1b[0J".' 1/50 [>---------------------------] 2%'.PHP_EOL.
"\x1b[1A\x1b[0J".' 2/50 [=>--------------------------] 4%'.PHP_EOL,
stream_get_contents($output->getStream())
Expand Down Expand Up @@ -434,7 +430,6 @@ public function testSetCurrentProgress()
rewind($output->getStream());
$this->assertEquals(
' 0/50 [>---------------------------] 0%'.
$this->generateOutput(' 0/50 [>---------------------------] 0%').
$this->generateOutput(' 1/50 [>---------------------------] 2%').
$this->generateOutput(' 15/50 [========>-------------------] 30%').
$this->generateOutput(' 25/50 [==============>-------------] 50%'),
Expand Down Expand Up @@ -541,7 +536,6 @@ public function testPercentNotHundredBeforeComplete()
rewind($output->getStream());
$this->assertEquals(
' 0/200 [>---------------------------] 0%'.
$this->generateOutput(' 0/200 [>---------------------------] 0%').
$this->generateOutput(' 199/200 [===========================>] 99%').
$this->generateOutput(' 200/200 [============================] 100%'),
stream_get_contents($output->getStream())
Expand Down Expand Up @@ -888,7 +882,6 @@ public function testIterate(): void
$this->assertEquals(
' 0/2 [>---------------------------] 0%'.
$this->generateOutput(' 1/2 [==============>-------------] 50%').
$this->generateOutput(' 2/2 [============================] 100%').
$this->generateOutput(' 2/2 [============================] 100%'),
stream_get_contents($output->getStream())
);
Expand Down Expand Up @@ -996,4 +989,18 @@ public function testPreventRedrawFasterThan()
stream_get_contents($output->getStream())
);
}

public function testNoWriteWhenMessageIsSame(): void
{
$bar = new ProgressBar($output = $this->getOutputStream(), 2);
$bar->start();
$bar->advance();
$bar->display();
rewind($output->getStream());
$this->assertEquals(
' 0/2 [>---------------------------] 0%'.
$this->generateOutput(' 1/2 [==============>-------------] 50%'),
stream_get_contents($output->getStream())
);
}
}

0 comments on commit 78b515f

Please sign in to comment.