Skip to content

Commit 0f57961

Browse files
Merge branch '6.1' into 6.2
* 6.1: [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 [CssSelector] Fix escape patterns
2 parents f3212cd + b800f23 commit 0f57961

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;
@@ -442,8 +441,6 @@ private function setRealFormat(string $format)
442441
} else {
443442
$this->format = $format;
444443
}
445-
446-
$this->formatLineCount = substr_count($this->format, "\n");
447444
}
448445

449446
/**
@@ -460,7 +457,7 @@ private function overwrite(string $message): void
460457
if ($this->overwrite) {
461458
if (null !== $this->previousMessage) {
462459
if ($this->output instanceof ConsoleSectionOutput) {
463-
$messageLines = explode("\n", $message);
460+
$messageLines = explode("\n", $this->previousMessage);
464461
$lineCount = \count($messageLines);
465462
foreach ($messageLines as $messageLine) {
466463
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
@@ -470,13 +467,11 @@ private function overwrite(string $message): void
470467
}
471468
$this->output->clear($lineCount);
472469
} else {
473-
if ('' !== $this->previousMessage) {
474-
// only clear upper lines when last call was not a clear
475-
for ($i = 0; $i < $this->formatLineCount; ++$i) {
476-
$this->cursor->moveToColumn(1);
477-
$this->cursor->clearLine();
478-
$this->cursor->moveUp();
479-
}
470+
$lineCount = substr_count($this->previousMessage, "\n");
471+
for ($i = 0; $i < $lineCount; ++$i) {
472+
$this->cursor->moveToColumn(1);
473+
$this->cursor->clearLine();
474+
$this->cursor->moveUp();
480475
}
481476

482477
$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)