Skip to content

Commit 9373481

Browse files
Merge branch '4.4' into 5.2
* 4.4: [Console] Fix line wrapping for decorated text in block output [Inflector] Fixed pluralize "coupon" [PhpUnitBridge] fix compat with symfony/debug [VarDumper] Adds support for ReflectionUnionType to VarDumper Correctly clear lines for multi-line progress bar messages.
2 parents a3528eb + 0a6f821 commit 9373481

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

Helper/ProgressBar.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,15 @@ private function overwrite(string $message): void
462462
if ($this->overwrite) {
463463
if (null !== $this->previousMessage) {
464464
if ($this->output instanceof ConsoleSectionOutput) {
465-
$lines = floor(Helper::strlenWithoutDecoration($this->output->getFormatter(), $message) / $this->terminal->getWidth()) + $this->formatLineCount + 1;
466-
$this->output->clear($lines);
465+
$messageLines = explode("\n", $message);
466+
$lineCount = \count($messageLines);
467+
foreach ($messageLines as $messageLine) {
468+
$messageLineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $messageLine);
469+
if ($messageLineLength > $this->terminal->getWidth()) {
470+
$lineCount += floor($messageLineLength / $this->terminal->getWidth());
471+
}
472+
}
473+
$this->output->clear($lineCount);
467474
} else {
468475
if ($this->formatLineCount > 0) {
469476
$this->cursor->moveUp($this->formatLineCount);

Style/SymfonyStyle.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,12 @@ private function createBlock(iterable $messages, string $type = null, string $st
476476
$message = OutputFormatter::escape($message);
477477
}
478478

479-
$lines = array_merge($lines, explode(\PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, \PHP_EOL, true)));
479+
$decorationLength = Helper::strlen($message) - Helper::strlenWithoutDecoration($this->getFormatter(), $message);
480+
$messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength);
481+
$messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true));
482+
foreach ($messageLines as $messageLine) {
483+
$lines[] = $messageLine;
484+
}
480485

481486
if (\count($messages) > 1 && $key < \count($messages) - 1) {
482487
$lines[] = '';
@@ -496,7 +501,9 @@ private function createBlock(iterable $messages, string $type = null, string $st
496501
}
497502

498503
$line = $prefix.$line;
499-
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
504+
$decorationLength = Helper::strlen($line) - Helper::strlenWithoutDecoration($this->getFormatter(), $line);
505+
$messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength);
506+
$line .= str_repeat(' ', max($this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line), 0));
500507

501508
if ($style) {
502509
$line = sprintf('<%s>%s</>', $style, $line);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
 // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 
3-
 // dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
4-
 // commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
5-
 // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
6-
 // id est laborum
2+
 // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore 
3+
 // magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 
4+
 // consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
5+
 // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
6+
 // est laborum
77

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)