Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6400b70

Browse filesBrowse files
committed
bug #18496 [Console] use ANSI escape sequences in ProgressBar overwrite method (alekitto)
This PR was squashed before being merged into the 2.7 branch (closes #18496). Discussion ---------- [Console] use ANSI escape sequences in ProgressBar overwrite method | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | maybe | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16957 | License | MIT | Doc PR | Rewritten `overwrite` method in ProgressBar class to use ANSI escape sequences to erase lines. This removes the need to store the last message length as it is not needed to fill the buffer with spaces anymore. As a plus it correctly resets the cursor position while clearing the output If the output is not decorated the behavior has not been changed. Could possibly cause a BC break if testing against the decorated emitted output as binary string Commits ------- b6cca4c [Console] use ANSI escape sequences in ProgressBar overwrite method
2 parents 6467b24 + b6cca4c commit 6400b70
Copy full SHA for 6400b70

File tree

Expand file treeCollapse file tree

2 files changed

+17
-33
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-33
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/ProgressBar.php
+11-27Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
+6-6Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.