8000 Correctly clear lines for multi-line progress bar messages. · symfony/symfony@8ada55c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ada55c

Browse files
committed
Correctly clear lines for multi-line progress bar messages.
1 parent a78fb18 commit 8ada55c

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