Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Prevent ProgressBar redraw when message is same #33884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
);
}
}