8000 bug #40460 Correctly clear lines for multi-line progress bar messages… · symfony/symfony@cf79189 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf79189

Browse files
committed
bug #40460 Correctly clear lines for multi-line progress bar messages (grasmash)
This PR was merged into the 4.4 branch. Discussion ---------- Correctly clear lines for multi-line progress bar messages Measure the length of individual lines when calculating progress bar message length. | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT When a multiline message is added to a progress bar, the progress bar measures the length of all lines combined in a single string without considering whether that string contains new line characters. It is not accurately measuring the number of lines that are displayed on the terminal. On the terminal, the actual number of new lines is dictated by a combination of new line characters and text extending beyond the terminal width. Due to this bug, the progress bar sometimes clears too many lines when the progress bar is advanced. The progress bar should instead consider the overlap of $this->formatLineCount and the actual number of lines in the message and then add any remainder. Commits ------- 8ada55c Correctly clear lines for multi-line progress bar messages.
2 parents a78fb18 + 8ada55c commit cf79189
< 8000 /div>

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,15 @@ private function overwrite(string $message): void
441441
if ($this->overwrite) {
442442
if (null !== $this->previousMessage) {
443443
if ($this->output instanceof ConsoleSectionOutput) {
444-
$lines = floor(Helper::strlenWithoutDecoration($this->output->getFormatter(), $message) / $this->terminal->getWidth()) + $this->formatLineCount + 1;
445-
$this->output->clear($lines);
444+
$messageLines = explode("\n", $message);
445+
$lineCount = \count($messageLines);
446+
foreach ($messageLines as $messageLine) {
447+
$messageLineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $messageLine);
448+
if ($messageLineLength > $this->terminal->getWidth()) {
449+
$lineCount += floor($messageLineLength / $this->terminal->getWidth());
450+
}
451+
}
452+
$this->output->clear($lineCount);
446453
} else {
447454
// Erase previous lines
448455
if ($this->formatLineCount > 0) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,34 @@ public function testOverwriteMultipleProgressBarsWithSectionOutputs()
397397
);
398398
}
399399

400+
public function testOverwriteWithSectionOutputWithNewlinesInMessage()
401+
{
402+
$sections = [];
403+
$stream = $this->getOutputStream(true);
404+
$output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter());
405+
406+
ProgressBar::setFormatDefinition('test', '%current%/%max% [%bar%] %percent:3s%% %message% Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.');
407+
408+
$bar = new ProgressBar($output, 50, 0);
409+
$bar->setFormat('test');
410+
$bar->start();
411+
$bar->display();
412+
$bar->setMessage("Twas brillig, and the slithy toves. Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.\nBeware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!");
413+
$bar->advance();
414+
$bar->setMessage("He took his vorpal sword in hand; Long time the manxome foe he sought— So rested he by the Tumtum tree And stood awhile in thought.\nAnd, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came!");
415+
$bar->advance();
416+
417+
rewind($output->getStream());
418+
$this->assertEquals(
419+
' 0/50 [>] 0% %message% Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.'.\PHP_EOL.
420+
"\x1b[6A\x1b[0J 1/50 [>] 2% Twas brillig, and the slithy toves. Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.
421+
Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch! Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.".\PHP_EOL.
422+
"\x1b[6A\x1b[0J 2/50 [>] 4% He took his vorpal sword in hand; Long time the manxome foe he sought— So rested he by the Tumtum tree And stood awhile in thought.
423+
And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came! Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.".\PHP_EOL,
424+
stream_get_contents($output->getStream())
425+
);
426+
}
427+
400428
public function testMultipleSectionsWithCustomFormat()
401429
{
402430
$sections = [];

0 commit comments

Comments
 (0)
0