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

Skip to content

Commit 40210a5

Browse files
committed
Prevent ProgressBar redraw when message is same
1 parent b43f255 commit 40210a5

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ final class ProgressBar
4747
private $overwrite = true;
4848
private $terminal;
4949
private $firstRun = true;
50+
private $currentMessage = '';
5051

5152
private static $formatters;
5253
private static $formats;
@@ -432,6 +433,12 @@ private function setRealFormat(string $format)
432433
*/
433434
private function overwrite(string $message): void
434435
{
436+
if ($this->currentMessage === $message) {
437+
return;
438+
}
439+
440+
$this->currentMessage = $message;
441+
435442
if ($this->overwrite) {
436443
if (!$this->firstRun) {
437444
if ($this->output instanceof ConsoleSectionOutput) {

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)