Skip to content

Commit 58422fd

Browse files
ncharalampidisnicolas-grekas
authored andcommitted
[Console] Correctly overwrite progressbars with different line count per step
1 parent 9bd719e commit 58422fd

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

Helper/ProgressBar.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ final class ProgressBar
5353
private $startTime;
5454
private $stepWidth;
5555
private $percent = 0.0;
56-
private $formatLineCount;
5756
private $messages = [];
5857
private $overwrite = true;
5958
private $terminal;
@@ -446,8 +445,6 @@ private function setRealFormat(string $format)
446445
} else {
447446
$this->format = $format;
448447
}
449-
450-
$this->formatLineCount = substr_count($this->format, "\n");
451448
}
452449

453450
/**
@@ -464,7 +461,7 @@ private function overwrite(string $message): void
464461
if ($this->overwrite) {
465462
if (null !== $this->previousMessage) {
466463
if ($this->output instanceof ConsoleSectionOutput) {
467-
$messageLines = explode("\n", $message);
464+
$messageLines = explode("\n", $this->previousMessage);
468465
$lineCount = \count($messageLines);
469466
foreach ($messageLines as $messageLine) {
470467
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
@@ -474,13 +471,11 @@ private function overwrite(string $message): void
474471
}
475472
$this->output->clear($lineCount);
476473
} else {
477-
if ('' !== $this->previousMessage) {
478-
// only clear upper lines when last call was not a clear
479-
for ($i = 0; $i < $this->formatLineCount; ++$i) {
480-
$this->cursor->moveToColumn(1);
481-
$this->cursor->clearLine();
482-
$this->cursor->moveUp();
483-
}
474+
$lineCount = substr_count($this->previousMessage, "\n");
475+
for ($i = 0; $i < $lineCount; ++$i) {
476+
$this->cursor->moveToColumn(1);
477+
$this->cursor->clearLine();
478+
$this->cursor->moveUp();
484479
}
485480

486481
$this->cursor->moveToColumn(1);

Tests/Helper/ProgressBarTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ public function testOverwriteWithAnsiSectionOutput()
361361
rewind($output->getStream());
362362
$this->assertSame(
363363
" \033[44;37m 0/50\033[0m [>---------------------------] 0%".\PHP_EOL.
364-
"\x1b[1A\x1b[0J"." \033[44;37m 1/50\033[0m [>---------------------------] 2%".\PHP_EOL.
365-
"\x1b[1A\x1b[0J"." \033[44;37m 2/50\033[0m [=>--------------------------] 4%".\PHP_EOL,
364+
"\x1b[1A\x1b[0J \033[44;37m 1/50\033[0m [>---------------------------] 2%".\PHP_EOL.
365+
"\x1b[1A\x1b[0J \033[44;37m 2/50\033[0m [=>--------------------------] 4%".\PHP_EOL,
366366
stream_get_contents($output->getStream())
367367
);
368368
putenv('COLUMNS=120');
@@ -397,6 +397,28 @@ public function testOverwriteMultipleProgressBarsWithSectionOutputs()
397397
);
398398
}
399399

400+
public function testOverwritWithNewlinesInMessage()
401+
{
402+
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.');
403+
404+
$bar = new ProgressBar($output = $this->getOutputStream(), 50, 0);
405+
$bar->setFormat('test');
406+
$bar->start();
407+
$bar->display();
408+
$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!");
409+
$bar->advance();
410+
$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!");
411+
$bar->advance();
412+
413+
rewind($output->getStream());
414+
$this->assertEquals(
415+
" 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.\x1b[1G\x1b[2K 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.
416+
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.\x1b[1G\x1b[2K\x1b[1A\x1b[1G\x1b[2K 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.
417+
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.",
418+
stream_get_contents($output->getStream())
419+
);
420+
}
421+
400422
public function testOverwriteWithSectionOutputWithNewlinesInMessage()
401423
{
402424
$sections = [];
@@ -417,7 +439,7 @@ public function testOverwriteWithSectionOutputWithNewlinesInMessage()
417439
rewind($output->getStream());
418440
$this->assertEquals(
419441
' 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.
442+
"\x1b[3A\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.
421443
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.
422444
"\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.
423445
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,

0 commit comments

Comments
 (0)