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

Skip to content

Commit e3b513b

Browse files
committed
minor #33884 Prevent ProgressBar redraw when message is same (fmasa)
This PR was squashed before being merged into the 4.4 branch (closes #33884). Discussion ---------- Prevent ProgressBar redraw when message is same | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This PR prevents ProgressBar from performing unnecessary redrawes if new output is same as current one. This is mostly useful when working with multiple progress bars. Same behavior can enforced by carefully setting redraw frequency, but I don't see any downsides for smarter redrawing by default. This can be moved to `if ($this->overwrite)` if necessary, so it's applied only in case overwriting is enabled. Commits ------- 78b515f Prevent ProgressBar redraw when message is same
2 parents 7db5be6 + 78b515f commit e3b513b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class ProgressBar
4646
private $messages = [];
4747
private $overwrite = true;
4848
private $terminal;
49-
private $firstRun = true;
49+
private $previousMessage;
5050

5151
private static $formatters;
5252
private static $formats;
@@ -432,8 +432,14 @@ private function setRealFormat(string $format)
432432
*/
433433
private function overwrite(string $message): void
434434
{
435+
if ($this->previousMessage === $message) {
436+
return;
437+
}
438+
439+
$originalMessage = $message;
440+
435441
if ($this->overwrite) {
436-
if (!$this->firstRun) {
442+
if (null !== $this->previousMessage) {
437443
if ($this->output instanceof ConsoleSectionOutput) {
438444
$lines = floor(Helper::strlen($message) / $this->terminal->getWidth()) + $this->formatLineCount + 1;
439445
$this->output->clear($lines);
@@ -451,7 +457,7 @@ private function overwrite(string $message): void
451457
$message = PHP_EOL.$message;
452458
}
453459

454-
$this->firstRun = false;
460+
$this->previousMessage = $originalMessage;
455461
$this->lastWriteTime = microtime(true);
456462

457463
$this->output->write($message);

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ public function testFormat()
187187
{
188188
$expected =
189189
' 0/10 [>---------------------------] 0%'.
190-
$this->generateOutput(' 10/10 [============================] 100%').
191190
$this->generateOutput(' 10/10 [============================] 100%')
192191
;
193192

@@ -296,7 +295,6 @@ public function testPercent()
296295
rewind($output->getStream());
297296
$this->assertEquals(
298297
' 0/50 [>---------------------------] 0%'.
299-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
300298
$this->generateOutput(' 1/50 [>---------------------------] 2%').
301299
$this->generateOutput(' 2/50 [=>--------------------------] 4%'),
302300
stream_get_contents($output->getStream())
@@ -318,7 +316,6 @@ public function testOverwriteWithShorterLine()
318316
rewind($output->getStream());
319317
$this->assertEquals(
320318
' 0/50 [>---------------------------] 0%'.
321-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
322319
$this->generateOutput(' 1/50 [>---------------------------] 2%').
323320
$this->generateOutput(' 2/50 [=>--------------------------]'),
324321
stream_get_contents($output->getStream())
@@ -340,7 +337,6 @@ public function testOverwriteWithSectionOutput()
340337
rewind($output->getStream());
341338
$this->assertEquals(
342339
' 0/50 [>---------------------------] 0%'.PHP_EOL.
343-
"\x1b[1A\x1b[0J".' 0/50 [>---------------------------] 0%'.PHP_EOL.
344340
"\x1b[1A\x1b[0J".' 1/50 [>---------------------------] 2%'.PHP_EOL.
345341
"\x1b[1A\x1b[0J".' 2/50 [=>--------------------------] 4%'.PHP_EOL,
346342
stream_get_contents($output->getStream())
@@ -434,7 +430,6 @@ public function testSetCurrentProgress()
434430
rewind($output->getStream());
435431
$this->assertEquals(
436432
' 0/50 [>---------------------------] 0%'.
437-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
438433
$this->generateOutput(' 1/50 [>---------------------------] 2%').
439434
$this->generateOutput(' 15/50 [========>-------------------] 30%').
440435
$this->generateOutput(' 25/50 [==============>-------------] 50%'),
@@ -541,7 +536,6 @@ public function testPercentNotHundredBeforeComplete()
541536
rewind($output->getStream());
542537
$this->assertEquals(
543538
' 0/200 [>---------------------------] 0%'.
544-
$this->generateOutput(' 0/200 [>---------------------------] 0%').
545539
$this->generateOutput(' 199/200 [===========================>] 99%').
546540
$this->generateOutput(' 200/200 [============================] 100%'),
547541
stream_get_contents($output->getStream())
@@ -888,7 +882,6 @@ public function testIterate(): void
888882
$this->assertEquals(
889883
' 0/2 [>---------------------------] 0%'.
890884
$this->generateOutput(' 1/2 [==============>-------------] 50%').
891-
$this->generateOutput(' 2/2 [============================] 100%').
892885
$this->generateOutput(' 2/2 [============================] 100%'),
893886
stream_get_contents($output->getStream())
894887
);
@@ -996,4 +989,18 @@ public function testPreventRedrawFasterThan()
996989
stream_get_contents($output->getStream())
997990
);
998991
}
992+
993+
public function testNoWriteWhenMessageIsSame(): void
994+
{
995+
$bar = new ProgressBar($output = $this->getOutputStream(), 2);
996+
$bar->start();
997+
$bar->advance();
998+
$bar->display();
999+
rewind($output->getStream());
1000+
$this->assertEquals(
1001+
' 0/2 [>---------------------------] 0%'.
1002+
$this->generateOutput(' 1/2 [==============>-------------] 50%'),
1003+
stream_get_contents($output->getStream())
1004+
);
1005+
}
9991006
}

0 commit comments

Comments
 (0)