Skip to content

Commit 3871e1a

Browse files
committed
Differentiate between the first time a progress bar is displayed and subsequent times
1 parent ec19a52 commit 3871e1a

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ProgressBar
4343
private $formatLineCount;
4444
private $messages = array();
4545
private $overwrite = true;
46+
private $firstRun = true;
4647

4748
private static $formatters;
4849
private static $formats;
@@ -522,20 +523,24 @@ private function setMaxSteps($max)
522523
private function overwrite($message)
523524
{
524525
if ($this->overwrite) {
525-
// Move the cursor to the beginning of the line
526-
$this->output->write("\x0D");
526+
if (!$this->isFirstRun()) {
527+
// Move the cursor to the beginning of the line
528+
$this->output->write("\x0D");
527529

528-
// Erase the line
529-
$this->output->write("\x1B[2K");
530+
// Erase the line
531+
$this->output->write("\x1B[2K");
530532

531-
// Erase previous lines
532-
if ($this->formatLineCount > 0) {
533-
$this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
533+
// Erase previous lines
534+
if ($this->formatLineCount > 0) {
535+
$this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
536+
}
534537
}
535538
} elseif ($this->step > 0) {
536539
$this->output->writeln('');
537540
}
538541

542+
$this->setFirstRun(false);
543+
539544
$this->output->write($message);
540545
}
541546

@@ -627,4 +632,14 @@ private static function initFormats()
627632
'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
628633
);
629634
}
635+
636+
private function isFirstRun()
637+
{
638+
return $this->firstRun;
639+
}
640+
641+
private function setFirstRun($firstRun)
642+
{
643+
$this->firstRun = (bool) $firstRun;
644+
}
630645
}

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testMultipleStart()
2929

3030
rewind($output->getStream());
3131
$this->assertEquals(
32-
$this->generateOutput(' 0 [>---------------------------]').
32+
' 0 [>---------------------------]'.
3333
$this->generateOutput(' 1 [->--------------------------]').
3434
$this->generateOutput(' 0 [>---------------------------]'),
3535
stream_get_contents($output->getStream())
@@ -44,7 +44,7 @@ public function testAdvance()
4444

4545
rewind($output->getStream());
4646
$this->assertEquals(
47-
$this->generateOutput(' 0 [>---------------------------]').
47+
' 0 [>---------------------------]'.
4848
$this->generateOutput(' 1 [->--------------------------]'),
4949
stream_get_contents($output->getStream())
5050
);
@@ -58,7 +58,7 @@ public function testAdvanceWithStep()
5858

5959
rewind($output->getStream());
6060
$this->assertEquals(
61-
$this->generateOutput(' 0 [>---------------------------]').
61+
' 0 [>---------------------------]'.
6262
$this->generateOutput(' 5 [----->----------------------]'),
6363
stream_get_contents($output->getStream())
6464
);
@@ -73,7 +73,7 @@ public function testAdvanceMultipleTimes()
7373

7474
rewind($output->getStream());
7575
$this->assertEquals(
76-
$this->generateOutput(' 0 [>---------------------------]').
76+
' 0 [>---------------------------]'.
7777
$this->generateOutput(' 3 [--->------------------------]').
7878
$this->generateOutput(' 5 [----->----------------------]'),
7979
stream_get_contents($output->getStream())
@@ -89,7 +89,7 @@ public function testAdvanceOverMax()
8989

9090
rewind($output->getStream());
9191
$this->assertEquals(
92-
$this->generateOutput(' 9/10 [=========================>--] 90%').
92+
' 9/10 [=========================>--] 90%'.
9393
$this->generateOutput(' 10/10 [============================] 100%').
9494
$this->generateOutput(' 11/11 [============================] 100%'),
9595
stream_get_contents($output->getStream())
@@ -99,7 +99,7 @@ public function testAdvanceOverMax()
9999
public function testFormat()
100100
{
101101
$expected =
102-
$this->generateOutput(' 0/10 [>---------------------------] 0%').
102+
' 0/10 [>---------------------------] 0%'.
103103
$this->generateOutput(' 10/10 [============================] 100%').
104104
$this->generateOutput(' 10/10 [============================] 100%')
105105
;
@@ -156,7 +156,7 @@ public function testCustomizations()
156156

157157
rewind($output->getStream());
158158
$this->assertEquals(
159-
$this->generateOutput(' 0/10 [/ ] 0%').
159+
' 0/10 [/ ] 0%'.
160160
$this->generateOutput(' 1/10 [_/ ] 10%'),
161161
stream_get_contents($output->getStream())
162162
);
@@ -169,7 +169,7 @@ public function testDisplayWithoutStart()
169169

170170
rewind($output->getStream());
171171
$this->assertEquals(
172-
$this->generateOutput(' 0/50 [>---------------------------] 0%'),
172+
' 0/50 [>---------------------------] 0%',
173173
stream_get_contents($output->getStream())
174174
);
175175
}
@@ -193,7 +193,7 @@ public function testFinishWithoutStart()
193193

194194
rewind($output->getStream());
195195
$this->assertEquals(
196-
$this->generateOutput(' 50/50 [============================] 100%'),
196+
' 50/50 [============================] 100%',
197197
stream_get_contents($output->getStream())
198198
);
199199
}
@@ -208,7 +208,7 @@ public function testPercent()
208208

209209
rewind($output->getStream());
210210
$this->assertEquals(
211-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
211+
' 0/50 [>---------------------------] 0%'.
212212
$this->generateOutput(' 0/50 [>---------------------------] 0%').
213213
$this->generateOutput(' 1/50 [>---------------------------] 2%').
214214
$this->generateOutput(' 2/50 [=>--------------------------] 4%'),
@@ -230,7 +230,7 @@ public function testOverwriteWithShorterLine()
230230

231231
rewind($output->getStream());
232232
$this->assertEquals(
233-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
233+
' 0/50 [>---------------------------] 0%'.
234234
$this->generateOutput(' 0/50 [>---------------------------] 0%').
235235
$this->generateOutput(' 1/50 [>---------------------------] 2%').
236236
$this->generateOutput(' 2/50 [=>--------------------------]'),
@@ -247,7 +247,7 @@ public function testStartWithMax()
247247

248248
rewind($output->getStream());
249249
$this->assertEquals(
250-
$this->generateOutput(' 0/50 [>---------------------------]').
250+
' 0/50 [>---------------------------]'.
251251
$this->generateOutput(' 1/50 [>---------------------------]'),
252252
stream_get_contents($output->getStream())
253253
);
@@ -264,7 +264,7 @@ public function testSetCurrentProgress()
264264

265265
rewind($output->getStream());
266266
$this->assertEquals(
267-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
267+
' 0/50 [>---------------------------] 0%'.
268268
$this->generateOutput(' 0/50 [>---------------------------] 0%').
269269
$this->generateOutput(' 1/50 [>---------------------------] 2%').
270270
$this->generateOutput(' 15/50 [========>-------------------] 30%').
@@ -339,7 +339,7 @@ public function testMultiByteSupport()
339339

340340
rewind($output->getStream());
341341
$this->assertEquals(
342-
$this->generateOutput(' 0 [>---------------------------]').
342+
' 0 [>---------------------------]'.
343343
$this->generateOutput(' 3 [■■■>------------------------]'),
344344
stream_get_contents($output->getStream())
345345
);
@@ -354,7 +354,7 @@ public function testClear()
354354

355355
rewind($output->getStream());
356356
$this->assertEquals(
357-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
357+
' 0/50 [>---------------------------] 0%'.
358358
$this->generateOutput(' 25/50 [==============>-------------] 50%').
359359
$this->generateOutput(''),
360360
stream_get_contents($output->getStream())
@@ -371,7 +371,7 @@ public function testPercentNotHundredBeforeComplete()
371371

372372
rewind($output->getStream());
373373
$this->assertEquals(
374-
$this->generateOutput(' 0/200 [>---------------------------] 0%').
374+
' 0/200 [>---------------------------] 0%'.
375375
$this->generateOutput(' 0/200 [>---------------------------] 0%').
376376
$this->generateOutput(' 199/200 [===========================>] 99%').
377377
$this->generateOutput(' 200/200 [============================] 100%'),
@@ -471,9 +471,9 @@ public function testParallelBars()
471471

472472
rewind($output->getStream());
473473
$this->assertEquals(
474-
$this->generateOutput(' 0/2 [>---------------------------] 0%')."\n".
475-
$this->generateOutput(' 0/3 [#---------------------------] 0%')."\n".
476-
rtrim($this->generateOutput(' 0 [>---------------------------]')).
474+
' 0/2 [>---------------------------] 0%'."\n".
475+
' 0/3 [#---------------------------] 0%'."\n".
476+
rtrim(' 0 [>---------------------------]').
477477

478478
"\033[2A".
479479
$this->generateOutput(' 1/2 [==============>-------------] 50%')."\n".
@@ -511,7 +511,7 @@ public function testWithoutMax()
511511

512512
rewind($output->getStream());
513513
$this->assertEquals(
514-
rtrim($this->generateOutput(' 0 [>---------------------------]')).
514+
rtrim(' 0 [>---------------------------]').
515515
rtrim($this->generateOutput(' 1 [->--------------------------]')).
516516
rtrim($this->generateOutput(' 2 [-->-------------------------]')).
517517
rtrim($this->generateOutput(' 3 [--->------------------------]')).
@@ -534,7 +534,7 @@ public function testAddingPlaceholderFormatter()
534534

535535
rewind($output->getStream());
536536
$this->assertEquals(
537-
$this->generateOutput(' 3 [>---------------------------]').
537+
' 3 [>---------------------------]'.
538538
$this->generateOutput(' 2 [=========>------------------]').
539539
$this->generateOutput(' 0 [============================]'),
540540
stream_get_contents($output->getStream())
@@ -553,7 +553,7 @@ public function testMultilineFormat()
553553

554554
rewind($output->getStream());
555555
$this->assertEquals(
556-
$this->generateOutput(">---------------------------\nfoobar").
556+
">---------------------------\nfoobar".
557557
$this->generateOutput("=========>------------------\nfoobar").
558558
"\x0D\x1B[2K\x1B[1A\x1B[2K".
559559
$this->generateOutput("============================\nfoobar"),
@@ -588,11 +588,11 @@ public function testAnsiColorsAndEmojis()
588588

589589
rewind($output->getStream());
590590
$this->assertEquals(
591-
$this->generateOutput(
591+
592592
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
593593
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
594594
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
595-
).
595+
.
596596
$this->generateOutput(
597597
" \033[44;37m Looks good to me... \033[0m\n".
598598
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
@@ -614,7 +614,7 @@ public function testSetFormat()
614614
$bar->start();
615615
rewind($output->getStream());
616616
$this->assertEquals(
617-
$this->generateOutput(' 0 [>---------------------------]'),
617+
' 0 [>---------------------------]',
618618
stream_get_contents($output->getStream())
619619
);
620620

@@ -623,7 +623,7 @@ public function testSetFormat()
623623
$bar->start();
624624
rewind($output->getStream());
625625
$this->assertEquals(
626-
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
626+
' 0/10 [>---------------------------] 0%',
627627
stream_get_contents($output->getStream())
628628
);
629629
}

0 commit comments

Comments
 (0)