diff --git a/Command/Command.php b/Command/Command.php index 65f54f801..66e8c3718 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -395,9 +395,9 @@ public function addArgument(string $name, int $mode = null, string $description /** * Adds an option. * - * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts - * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants - * @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE) + * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts + * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants + * @param string|string[]|bool|null $default The default value (must be null for InputOption::VALUE_NONE) * * @throws InvalidArgumentException If option mode is invalid or incompatible * diff --git a/Helper/Helper.php b/Helper/Helper.php index f82dd286f..acec994db 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Helper; use Symfony\Component\Console\Formatter\OutputFormatterInterface; +use Symfony\Component\String\UnicodeString; /** * Helper is the base class for all helper classes. @@ -45,7 +46,11 @@ public function getHelperSet() */ public static function strlen(?string $string) { - $string = (string) $string; + $string ?? $string = ''; + + if (preg_match('//u', $string)) { + return (new UnicodeString($string))->width(false); + } if (false === $encoding = mb_detect_encoding($string, null, true)) { return \strlen($string); @@ -59,9 +64,9 @@ public static function strlen(?string $string) * * @return string The string subset */ - public static function substr(string $string, int $from, int $length = null) + public static function substr(?string $string, int $from, int $length = null) { - $string = (string) $string; + $string ?? $string = ''; if (false === $encoding = mb_detect_encoding($string, null, true)) { return substr($string, $from, $length); @@ -116,17 +121,23 @@ public static function formatMemory(int $memory) return sprintf('%d B', $memory); } - public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string) + public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) { - return self::strlen(self::removeDecoration($formatter, $string)); + $string = self::removeDecoration($formatter, $string); + + if (preg_match('//u', $string)) { + return (new UnicodeString($string))->width(true); + } + + return self::strlen($string); } - public static function removeDecoration(OutputFormatterInterface $formatter, $string) + public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) { $isDecorated = $formatter->isDecorated(); $formatter->setDecorated(false); // remove <...> formatting - $string = $formatter->format($string); + $string = $formatter->format($string ?? ''); // remove already formatted characters $string = preg_replace("/\033\[[^m]*m/", '', $string); $formatter->setDecorated($isDecorated); diff --git a/Helper/ProgressBar.php b/Helper/ProgressBar.php index a3aa48097..61c471424 100644 --- a/Helper/ProgressBar.php +++ b/Helper/ProgressBar.php @@ -462,8 +462,15 @@ private function overwrite(string $message): void if ($this->overwrite) { if (null !== $this->previousMessage) { if ($this->output instanceof ConsoleSectionOutput) { - $lines = floor(Helper::strlen($message) / $this->terminal->getWidth()) + $this->formatLineCount + 1; - $this->output->clear($lines); + $messageLines = explode("\n", $message); + $lineCount = \count($messageLines); + foreach ($messageLines as $messageLine) { + $messageLineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $messageLine); + if ($messageLineLength > $this->terminal->getWidth()) { + $lineCount += floor($messageLineLength / $this->terminal->getWidth()); + } + } + $this->output->clear($lineCount); } else { if ($this->formatLineCount > 0) { $this->cursor->moveUp($this->formatLineCount); diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index c0fb5461f..5bf8186b8 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -311,7 +311,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu $remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)))); $output->write($remainingCharacters); $fullChoice .= $remainingCharacters; - $i = self::strlen($fullChoice); + $i = (false === $encoding = mb_detect_encoding($fullChoice, null, true)) ? \strlen($fullChoice) : mb_strlen($fullChoice, $encoding); $matches = array_filter( $autocomplete($ret), diff --git a/Input/ArrayInput.php b/Input/ArrayInput.php index 66f0bedc3..5c1e2f63a 100644 --- a/Input/ArrayInput.php +++ b/Input/ArrayInput.php @@ -108,12 +108,13 @@ public function __toString() $params = []; foreach ($this->parameters as $param => $val) { if ($param && \is_string($param) && '-' === $param[0]) { + $glue = ('-' === $param[1]) ? '=' : ' '; if (\is_array($val)) { foreach ($val as $v) { - $params[] = $param.('' != $v ? '='.$this->escapeToken($v) : ''); + $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); } } else { - $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); + $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); } } else { $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); diff --git a/Input/InputOption.php b/Input/InputOption.php index 66f857a6c..a8e956db5 100644 --- a/Input/InputOption.php +++ b/Input/InputOption.php @@ -33,11 +33,11 @@ class InputOption private $description; /** - * @param string $name The option name - * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts - * @param int|null $mode The option mode: One of the VALUE_* constants - * @param string $description A description text - * @param string|string[]|int|bool|null $default The default value (must be null for self::VALUE_NONE) + * @param string $name The option name + * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts + * @param int|null $mode The option mode: One of the VALUE_* constants + * @param string $description A description text + * @param string|string[]|bool|null $default The default value (must be null for self::VALUE_NONE) * * @throws InvalidArgumentException If option mode is invalid or incompatible */ @@ -149,7 +149,7 @@ public function isArray() /** * Sets the default value. * - * @param string|string[]|int|bool|null $default The default value + * @param string|string[]|bool|null $default The default value * * @throws LogicException When incorrect default value is given */ @@ -173,7 +173,7 @@ public function setDefault($default = null) /** * Returns the default value. * - * @return string|string[]|int|bool|null The default value + * @return string|string[]|bool|null The default value */ public function getDefault() { diff --git a/Style/SymfonyStyle.php b/Style/SymfonyStyle.php index a1b019ca0..075fe6621 100644 --- a/Style/SymfonyStyle.php +++ b/Style/SymfonyStyle.php @@ -476,7 +476,12 @@ private function createBlock(iterable $messages, string $type = null, string $st $message = OutputFormatter::escape($message); } - $lines = array_merge($lines, explode(\PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, \PHP_EOL, true))); + $decorationLength = Helper::strlen($message) - Helper::strlenWithoutDecoration($this->getFormatter(), $message); + $messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength); + $messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true)); + foreach ($messageLines as $messageLine) { + $lines[] = $messageLine; + } if (\count($messages) > 1 && $key < \count($messages) - 1) { $lines[] = ''; @@ -496,7 +501,7 @@ private function createBlock(iterable $messages, string $type = null, string $st } $line = $prefix.$line; - $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line)); + $line .= str_repeat(' ', max($this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line), 0)); if ($style) { $line = sprintf('<%s>%s', $style, $line); diff --git a/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php b/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php new file mode 100644 index 000000000..8460e7ece --- /dev/null +++ b/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php @@ -0,0 +1,13 @@ +success('Lorem ipsum dolor sit amet'); + $output->success('Lorem ipsum dolor sit amet with one emoji πŸŽ‰'); + $output->success('Lorem ipsum dolor sit amet with so many of them πŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎ'); +}; diff --git a/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt b/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt index 0f3704b74..ea8e4351e 100644 --- a/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt +++ b/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt @@ -1,7 +1,7 @@ - // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et  - // dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea  - // commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla  - // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim - // id est laborum + // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore  + // magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  + // consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla  + // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + // est laborum diff --git a/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt b/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt new file mode 100644 index 000000000..aee3c4a89 --- /dev/null +++ b/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt @@ -0,0 +1,7 @@ + + [OK] Lorem ipsum dolor sit amet + + [OK] Lorem ipsum dolor sit amet with one emoji πŸŽ‰ + + [OK] Lorem ipsum dolor sit amet with so many of them πŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎ + diff --git a/Tests/Helper/ProgressBarTest.php b/Tests/Helper/ProgressBarTest.php index 24c69789c..8baf5a6d8 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -343,6 +343,31 @@ public function testOverwriteWithSectionOutput() ); } + public function testOverwriteWithAnsiSectionOutput() + { + // output has 43 visible characters plus 2 invisible ANSI characters + putenv('COLUMNS=43'); + $sections = []; + $stream = $this->getOutputStream(true); + $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); + + $bar = new ProgressBar($output, 50, 0); + $bar->setFormat(" \033[44;37m%current%/%max%\033[0m [%bar%] %percent:3s%%"); + $bar->start(); + $bar->display(); + $bar->advance(); + $bar->advance(); + + rewind($output->getStream()); + $this->assertSame( + " \033[44;37m 0/50\033[0m [>---------------------------] 0%".\PHP_EOL. + "\x1b[1A\x1b[0J"." \033[44;37m 1/50\033[0m [>---------------------------] 2%".\PHP_EOL. + "\x1b[1A\x1b[0J"." \033[44;37m 2/50\033[0m [=>--------------------------] 4%".\PHP_EOL, + stream_get_contents($output->getStream()) + ); + putenv('COLUMNS=120'); + } + public function testOverwriteMultipleProgressBarsWithSectionOutputs() { $sections = []; @@ -372,6 +397,34 @@ public function testOverwriteMultipleProgressBarsWithSectionOutputs() ); } + public function testOverwriteWithSectionOutputWithNewlinesInMessage() + { + $sections = []; + $stream = $this->getOutputStream(true); + $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); + + 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.'); + + $bar = new ProgressBar($output, 50, 0); + $bar->setFormat('test'); + $bar->start(); + $bar->display(); + $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!"); + $bar->advance(); + $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!"); + $bar->advance(); + + rewind($output->getStream()); + $this->assertEquals( + ' 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. + "\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. +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. + "\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. +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, + stream_get_contents($output->getStream()) + ); + } + public function testMultipleSectionsWithCustomFormat() { $sections = []; diff --git a/Tests/Input/ArrayInputTest.php b/Tests/Input/ArrayInputTest.php index f3eedb3a2..5777c44b7 100644 --- a/Tests/Input/ArrayInputTest.php +++ b/Tests/Input/ArrayInputTest.php @@ -162,10 +162,10 @@ public function provideInvalidInput() public function testToString() { $input = new ArrayInput(['-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"]); - $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input); + $this->assertEquals('-f -b bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input); $input = new ArrayInput(['-b' => ['bval_1', 'bval_2'], '--f' => ['fval_1', 'fval_2']]); - $this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input); + $this->assertSame('-b bval_1 -b bval_2 --f=fval_1 --f=fval_2', (string) $input); $input = new ArrayInput(['array_arg' => ['val_1', 'val_2']]); $this->assertSame('val_1 val_2', (string) $input);