Skip to content

Commit 2ab3073

Browse files
Merge branch '5.4' into 6.0
* 5.4: [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 9050b8c + 58422fd commit 2ab3073

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 int $startTime;
5454
private int $stepWidth;
5555
private float $percent = 0.0;
56-
private int $formatLineCount;
5756
private array $messages = [];
5857
private bool $overwrite = true;
5958
private $terminal;
@@ -438,8 +437,6 @@ private function setRealFormat(string $format)
438437
} else {
439438
$this->format = $format;
440439
}
441-
442-
$this->formatLineCount = substr_count($this->format, "\n");
443440
}
444441

445442
/**
@@ -456,7 +453,7 @@ private function overwrite(string $message): void
456453
if ($this->overwrite) {
457454
if (null !== $this->previousMessage) {
458455
if ($this->output instanceof ConsoleSectionOutput) {
459-
$messageLines = explode("\n", $message);
456+
$messageLines = explode("\n", $this->previousMessage);
460457
$lineCount = \count($messageLines);
461458
foreach ($messageLines as $messageLine) {
462459
$messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
@@ -466,13 +463,11 @@ private function overwrite(string $message): void
466463
}
467464
$this->output->clear($lineCount);
468465
} else {
469-
if ('' !== $this->previousMessage) {
470-
// only clear upper lines when last call was not a clear
471-
for ($i = 0; $i < $this->formatLineCount; ++$i) {
472-
$this->cursor->moveToColumn(1);
473-
$this->cursor->clearLine();
474-
$this->cursor->moveUp();
475-
}
466+
$lineCount = substr_count($this->previousMessage, "\n");
467+
for ($i = 0; $i < $lineCount; ++$i) {
468+
$this->cursor->moveToColumn(1);
469+
$this->cursor->clearLine();
470+
$this->cursor->moveUp();
476471
}
477472

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