8000 Differentiate between the first time a progress bar is displayed and … · symfony/symfony@1ca75ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ca75ad

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

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

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

Lines changed: 28 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,20 @@ private static function initFormats()
627632
'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
628633
);
629634
}
635+
636+
/**
637+
* @return bool
638+
*/
639+
protected function isFirstRun()
640+
{
641+
return $this->firstRun;
642+
}
643+
644+
/**
645+
* @param bool $firstRun
646+
*/
647+
protected function setFirstRun($firstRun)
648+
{
649+
$this->firstRun = (bool) $firstRun;
650+
}
630651
}

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
/**
1919
* @group time-sensitive
20+
* @group pb
2021
*/
2122
class ProgressBarTest extends \PHPUnit_Framework_TestCase
2223
{
@@ -29,7 +30,7 @@ public function testMultipleStart()
2930

3031
rewind($output->getStream());
3132
$this->assertEquals(
32-
$this->generateOutput(' 0 [>---------------------------]').
33+
' 0 [>---------------------------]'.
3334
$this->generateOutput(' 1 [->--------------------------]').
3435
$this->generateOutput(' 0 [>---------------------------]'),
3536
stream_get_contents($output->getStream())
@@ -44,7 +45,7 @@ public function testAdvance()
4445

4546
rewind($output->getStream());
4647
$this->assertEquals(
47-
$this->generateOutput(' 0 [>---------------------------]').
48+
' 0 [>---------------------------]'.
4849
$this->generateOutput(' 1 [->--------------------------]'),
4950
stream_get_contents($output->getStream())
5051
);
@@ -58,7 +59,7 @@ public function testAdvanceWithStep()
5859

5960
rewind($output->getStream());
6061
$this->assertEquals(
61-
$this->generateOutput(' 0 [>---------------------------]').
62+
' 0 [>---------------------------]'.
6263
$this->generateOutput(' 5 [----->----------------------]'),
6364
stream_get_contents($output->getStream())
6465
);
@@ -73,7 +74,7 @@ public function testAdvanceMultipleTimes()
7374

7475
rewind($output->getStream());
7576
$this->assertEquals(
76-
$this->generateOutput(' 0 [>---------------------------]').
77+
' 0 [>---------------------------]'.
7778
$this->generateOutput(' 3 [--->------------------------]').
7879
$this->generateOutput(' 5 [----->----------------------]'),
7980
stream_get_contents($output->getStream())
@@ -89,7 +90,7 @@ public function testAdvanceOverMax()
8990

9091
rewind($output->getStream());
9192
$this->assertEquals(
92-
$this->generateOutput(' 9/10 [=========================>--] 90%').
93+
' 9/10 [=========================>--] 90%'.
9394
$this->generateOutput(' 10/10 [============================] 100%').
9495
$this->generateOutput(' 11/11 [============================] 100%'),
9596
stream_get_contents($output->getStream())
@@ -99,7 +100,7 @@ public function testAdvanceOverMax()
99100
public function testFormat()
100101
{
101102
$expected =
102-
$this->generateOutput(' 0/10 [>---------------------------] 0%').
103+
' 0/10 [>---------------------------] 0%'.
103104
$this->generateOutput(' 10/10 [============================] 100%').
104105
$this->generateOutput(' 10/10 [============================] 100%')
105106
;
@@ -156,7 +157,7 @@ public function testCustomizations()
156157

157158
rewind($output->getStream());
158159
$this->assertEquals(
159-
$this->generateOutput(' 0/10 [/ ] 0%').
160+
' 0/10 [/ ] 0%'.
160161
$this->generateOutput(' 1/10 [_/ ] 10%'),
161162
stream_get_contents($output->getStream())
162163
);
@@ -169,7 +170,7 @@ public function testDisplayWithoutStart()
169170

170171
rewind($output->getStream());
171172
$this->assertEquals(
172-
$this->generateOutput(' 0/50 [>---------------------------] 0%'),
173+
' 0/50 [>---------------------------] 0%',
173174
stream_get_contents($output->getStream())
174175
);
175176
}
@@ -193,7 +194,7 @@ public function testFinishWithoutStart()
193194

194195
rewind($output->getStream());
195196
$this->assertEquals(
196-
$this->generateOutput(' 50/50 [============================] 100%'),
197+
' 50/50 [============================] 100%',
197198
stream_get_contents($output->getStream())
198199
);
199200
}
@@ -208,7 +209,7 @@ public function testPercent()
208209

209210
rewind($output->getStream());
210211
$this->assertEquals(
211-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
212+
' 0/50 [>---------------------------] 0%'.
212213
$this->generateOutput(' 0/50 [>---------------------------] 0%').
213214
$this->generateOutput(' 1/50 [>---------------------------] 2%').
214215
$this->generateOutput(' 2/50 [=>--------------------------] 4%'),
@@ -230,7 +231,7 @@ public function testOverwriteWithShorterLine()
230231

231232
rewind($output->getStream());
232233
$this->assertEquals(
233-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
234+
' 0/50 [>---------------------------] 0%'.
234235
$this->generateOutput(' 0/50 [>---------------------------] 0%').
235236
$this->generateOutput(' 1/50 [>---------------------------] 2%').
236237
$this->generateOutput(' 2/50 [=>--------------------------]'),
@@ -247,7 +248,7 @@ public function testStartWithMax()
247248

248249
rewind($output->getStream());
249250
$this->assertEquals(
250-
$this->generateOutput(' 0/50 [>---------------------------]').
251+
' 0/50 [>---------------------------]'.
251252
$this->generateOutput(' 1/50 [>---------------------------]'),
252253
stream_get_contents($output->getStream())
253254
);
@@ -264,7 +265,7 @@ public function testSetCurrentProgress()
264265

265266
rewind($output->getStream());
266267
$this->assertEquals(
267-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
268+
' 0/50 [>---------------------------] 0%'.
268269
$this->generateOutput(' 0/50 [>---------------------------] 0%').
269270
$this->generateOutput(' 1/50 [>---------------------------] 2%').
270271
$this->generateOutput(' 15/50 [========>-------------------] 30%').
@@ -339,7 +340,7 @@ public function testMultiByteSupport()
339340

340341
rewind($output->getStream());
341342
$this->assertEquals(
342-
$this->generateOutput(' 0 [>---------------------------]').
343+
' 0 [>---------------------------]'.
343344
$this->generateOutput(' 3 [■■■>------------------------]'),
344345
stream_get_contents($output->getStream())
345346
);
@@ -354,7 +355,7 @@ public function testClear()
354355

355356
rewind($output->getStream());
356357
$this->assertEquals(
357-
$this->generateOutput(' 0/50 [>---------------------------] 0%').
358+
' 0/50 [>---------------------------] 0%'.
358359
$this->generateOutput(' 25/50 [==============>-------------] 50%').
359360
$this->generateOutput(''),
360361
stream_get_contents($output->getStream())
@@ -371,7 +372,7 @@ public function testPercentNotHundredBeforeComplete()
371372

372373
rewind($output->getStream());
373374
$this->assertEquals(
374-
$this->generateOutput(' 0/200 [>---------------------------] 0%').
375+
' 0/200 [>---------------------------] 0%'.
375376
$this->generateOutput(' 0/200 [>---------------------------] 0%').
376377
$this->generateOutput(' 199/200 [===========================>] 99%').
377378
$this->generateOutput(' 200/200 [============================] 100%'),
@@ -471,9 +472,9 @@ public function testParallelBars()
471472

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

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

512513
rewind($output->getStream());
513514
$this->assertEquals(
514-
rtrim($this->generateOutput(' 0 [>---------------------------]')).
515+
rtrim(' 0 [>---------------------------]').
515516
rtrim($this->generateOutput(' 1 [->--------------------------]')).
516517
rtrim($this->generateOutput(' 2 [-->-------------------------]')).
517518
rtrim($this->generateOutput(' 3 [--->------------------------]')).
@@ -534,7 +535,7 @@ public function testAddingPlaceholderFormatter()
534535

535536
rewind($output->getStream());
536537
$this->assertEquals(
537-
$this->generateOutput(' 3 [>---------------------------]').
538+
' 3 [>---------------------------]'.
538539
$this->generateOutput(' 2 [=========>------------------]').
539540
$this->generateOutput(' 0 [============================]'),
540541
stream_get_contents($output->getStream())
@@ -553,7 +554,7 @@ public function testMultilineFormat()
553554

554555
rewind($output->getStream());
555556
$this->assertEquals(
556-
$this->generateOutput(">---------------------------\nfoobar").
557+
">---------------------------\nfoobar".
557558
$this->generateOutput("=========>------------------\nfoobar").
558559
"\x0D\x1B[2K\x1B[1A\x1B[2K".
559560
$this->generateOutput("============================\nfoobar"),
@@ -588,11 +589,11 @@ public function testAnsiColorsAndEmojis()
588589

589590
rewind($output->getStream());
590591
$this->assertEquals(
591-
$this->generateOutput(
592+
592593
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
593594
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
594595
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
595-
).
596+
.
596597
$this->generateOutput(
597598
" \033[44;37m Looks good to me... \033[0m\n".
598599
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
@@ -614,7 +615,7 @@ public function testSetFormat()
614615
$bar->start();
615616
rewind($output->getStream());
616617
$this->assertEquals(
617-
$this->generateOutput(' 0 [>---------------------------]'),
618+
' 0 [>---------------------------]',
618619
stream_get_contents($output->getStream())
619620
);
620621

@@ -623,7 +624,7 @@ public function testSetFormat()
623624
$bar->start();
624625
rewind($output->getStream());
625626
$this->assertEquals(
626-
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
627+
' 0/10 [>---------------------------] 0%',
627628
stream_get_contents($output->getStream())
628629
);
629630
}

0 commit comments

Comments
 (0)
0