Skip to content

Commit 8e5bc2d

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Console] Correctly overwrite progressbars with different line count per step [DependencyInjection] Fix deduplicating service instances in circular graphs [Form] Make `ButtonType` handle `form_attr` option [PhpUnitBridge] Use verbose deprecation output for quiet types only when it reaches the threshold [WebProfilerBundle] fix Mailer detail on click [Translation] Fix undefined variable messages in ConstraintVisitor [CssSelector] Fix escape patterns [PropertyAccess] Fix nullsafe chain like x?.y
2 parents dc9d4fa + 0f57961 commit 8e5bc2d

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
@@ -54,7 +54,6 @@ final class ProgressBar
5454
private int $startTime;
5555
private int $stepWidth;
5656
private float $percent = 0.0;
57-
private int $formatLineCount;
5857
private array $messages = [];
5958
private bool $overwrite = true;
6059
private Terminal $terminal;
@@ -463,8 +462,6 @@ private function setRealFormat(string $format)
463462
} else {
464463
$this->format = $format;
465464
}
466-
467-
$this->formatLineCount = substr_count($this->format, "\n");
468465
}
469466

470467
/**
@@ -481,7 +478,7 @@ private function overwrite(string $message): void
481478
if ($this->overwrite) {
482479
if (null !== $this->previousMessage) {
483480
if ($this->output instanceof ConsoleSectionOutput) {
484-
$messageLines = explode("\n", $message);
481+
$messageLines = explode("\n", $this->previousMessage);
485482
$lineCount = \count($messageLines);
486483
foreach ($messageLines as $messageLine) {
487484
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
@@ -491,13 +488,11 @@ private function overwrite(string $message): void
491488
}
492489
$this->output->clear($lineCount);
493490
} else {
494-
if ('' !== $this->previousMessage) {
495-
// only clear upper lines when last call was not a clear
496-
for ($i = 0; $i < $this->formatLineCount; ++$i) {
497-
$this->cursor->moveToColumn(1);
498-
$this->cursor->clearLine();
499-
$this->cursor->moveUp();
500-
}
491+
$lineCount = substr_count($this->previousMessage, "\n");
492+
for ($i = 0; $i < $lineCount; ++$i) {
493+
$this->cursor->moveToColumn(1);
494+
$this->cursor->clearLine();
495+
$this->cursor->moveUp();
501496
}
502497

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

Tests/Helper/ProgressBarTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ public function testOverwriteWithAnsiSectionOutput()
424424
rewind($output->getStream());
425425
$this->assertSame(
426426
" \033[44;37m 0/50\033[0m [>---------------------------] 0%".\PHP_EOL.
427-
"\x1b[1A\x1b[0J"." \033[44;37m 1/50\033[0m [>---------------------------] 2%".\PHP_EOL.
428-
"\x1b[1A\x1b[0J"." \033[44;37m 2/50\033[0m [=>--------------------------] 4%".\PHP_EOL,
427+
"\x1b[1A\x1b[0J \033[44;37m 1/50\033[0m [>---------------------------] 2%".\PHP_EOL.
428+
"\x1b[1A\x1b[0J \033[44;37m 2/50\033[0m [=>--------------------------] 4%".\PHP_EOL,
429429
stream_get_contents($output->getStream())
430430
);
431431
putenv('COLUMNS=120');
@@ -460,6 +460,28 @@ public function testOverwriteMultipleProgressBarsWithSectionOutputs()
460460
);
461461
}
462462

463+
public function testOverwritWithNewlinesInMessage()
464+
{
465+
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.');
466+
467+
$bar = new ProgressBar($output = $this->getOutputStream(), 50, 0);
468+
$bar->setFormat('test');
469+
$bar->start();
470+
$bar->display();
471+
$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!");
472+
$bar->advance();
473+
$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!");
474+
$bar->advance();
475+
476+
rewind($output->getStream());
477+
$this->assertEquals(
478+
" 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.
479+
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.
480+
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.",
481+
stream_get_contents($output->getStream())
482+
);
483+
}
484+
463485
public function testOverwriteWithSectionOutputWithNewlinesInMessage()
464486
{
465487
$sections = [];
@@ -480,7 +502,7 @@ public function testOverwriteWithSectionOutputWithNewlinesInMessage()
480502
rewind($output->getStream());
481503
$this->assertEquals(
482504
' 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.
483-
"\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.
505+
"\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.
484506
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.
485507
"\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.
486508
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)