Skip to content

Commit b6cca4c

Browse files
alekittofabpot
authored andcommitted
[Console] use ANSI escape sequences in ProgressBar overwrite method
1 parent 064aedf commit b6cca4c

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class ProgressBar
4040
private $startTime;
4141
private $stepWidth;
4242
private $percent = 0.0;
43-
private $lastMessagesLength = 0;
4443
private $formatLineCount;
4544
private $messages;
4645
private $overwrite = true;
@@ -472,7 +471,7 @@ public function clear()
472471
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
473472
}
474473

475-
$this->overwrite(str_repeat("\n", $this->formatLineCount));
474+
$this->overwrite('');
476475
}
477476

478477
/**
@@ -512,37 +511,22 @@ private function setMaxSteps($max)
512511
*/
513512
private function overwrite($message)
514513
{
515-
$lines = explode("\n", $message);
516-
517-
// append whitespace to match the line's length
518-
if (null !== $this->lastMessagesLength) {
519-
foreach ($lines as $i => $line) {
520-
if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $line)) {
521-
$lines[$i] = str_pad($line, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
522-
}
523-
}
524-
}
525-
526514
if ($this->overwrite) {
527-
// move back to the beginning of the progress bar before redrawing it
515+
// Move the cursor to the beginning of the line
528516
$this->output->write("\x0D");
529-
} elseif ($this->step > 0) {
530-
// move to new line
531-
$this->output->writeln('');
532-
}
533517

534-
if ($this->formatLineCount) {
535-
$this->output->write(sprintf("\033[%dA", $this->formatLineCount));
536-
}
537-
$this->output->write(implode("\n", $lines));
518+
// Erase the line
519+
$this->output->write("\x1B[2K");
538520

539-
$this->lastMessagesLength = 0;
540-
foreach ($lines as $line) {
541-
$len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
542-
if ($len > $this->lastMessagesLength) {
543-
$this->lastMessagesLength = $len;
521+
// Erase previous lines
522+
if ($this->formatLineCount > 0) {
523+
$this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
544524
}
525+
} elseif ($this->step > 0) {
526+
$this->output->writeln('');
545527
}
528+
529+
$this->output->write($message);
546530
}
547531

548532
private function determineBestFormat()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function testOverwriteWithShorterLine()
233233
$this->generateOutput(' 0/50 [>---------------------------] 0%').
234234
$this->generateOutput(' 0/50 [>---------------------------] 0%').
235235
$this->generateOutput(' 1/50 [>---------------------------] 2%').
236-
$this->generateOutput(' 2/50 [=>--------------------------] '),
236+
$this->generateOutput(' 2/50 [=>--------------------------]'),
237237
stream_get_contents($output->getStream())
238238
);
239239
}
@@ -356,7 +356,7 @@ public function testClear()
356356
$this->assertEquals(
357357
$this->generateOutput(' 0/50 [>---------------------------] 0%').
358358
$this->generateOutput(' 25/50 [==============>-------------] 50%').
359-
$this->generateOutput(' '),
359+
$this->generateOutput(''),
360360
stream_get_contents($output->getStream())
361361
);
362362
}
@@ -554,9 +554,9 @@ public function testMultilineFormat()
554554
rewind($output->getStream());
555555
$this->assertEquals(
556556
$this->generateOutput(">---------------------------\nfoobar").
557-
$this->generateOutput("=========>------------------\nfoobar ").
558-
$this->generateOutput(" \n ").
559-
$this->generateOutput("============================\nfoobar "),
557+
$this->generateOutput("=========>------------------\nfoobar").
558+
"\x0D\x1B[2K\x1B[1A\x1B[2K".
559+
$this->generateOutput("============================\nfoobar"),
560560
stream_get_contents($output->getStream())
561561
);
562562
}
@@ -665,6 +665,6 @@ protected function generateOutput($expected)
665665
{
666666
$count = substr_count($expected, "\n");
667667

668-
return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected;
668+
return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
669669
}
670670
}

0 commit comments

Comments
 (0)