From 7efe40ad2dfca14afa9bde2538201aa3de9d6e66 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 3 Apr 2021 13:34:12 +0200 Subject: [PATCH 1/2] Revert "[Command] fix emojis messing up the line width" This reverts commit 36b36dcecc8223b8f3d02c6c463a9a2dbf48ff90. --- .../Component/Console/Helper/Helper.php | 25 ++++++------------- .../Console/Helper/QuestionHelper.php | 2 +- .../Component/Console/Style/SymfonyStyle.php | 2 ++ .../Style/SymfonyStyle/command/command_21.php | 13 ---------- .../Style/SymfonyStyle/output/output_21.txt | 7 ------ 5 files changed, 10 insertions(+), 39 deletions(-) delete mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php delete mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt diff --git a/src/Symfony/Component/Console/Helper/Helper.php b/src/Symfony/Component/Console/Helper/Helper.php index acec994db83c4..f82dd286fc3b5 100644 --- a/src/Symfony/Component/Console/Helper/Helper.php +++ b/src/Symfony/Component/Console/Helper/Helper.php @@ -12,7 +12,6 @@ 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. @@ -46,11 +45,7 @@ public function getHelperSet() */ public static function strlen(?string $string) { - $string ?? $string = ''; - - if (preg_match('//u', $string)) { - return (new UnicodeString($string))->width(false); - } + $string = (string) $string; if (false === $encoding = mb_detect_encoding($string, null, true)) { return \strlen($string); @@ -64,9 +59,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); @@ -121,23 +116,17 @@ public static function formatMemory(int $memory) return sprintf('%d B', $memory); } - public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) + public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string) { - $string = self::removeDecoration($formatter, $string); - - if (preg_match('//u', $string)) { - return (new UnicodeString($string))->width(true); - } - - return self::strlen($string); + return self::strlen(self::removeDecoration($formatter, $string)); } - public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) + public static function removeDecoration(OutputFormatterInterface $formatter, $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/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 5bf8186b8fbf8..c0fb5461fc660 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/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 = (false === $encoding = mb_detect_encoding($fullChoice, null, true)) ? \strlen($fullChoice) : mb_strlen($fullChoice, $encoding); + $i = self::strlen($fullChoice); $matches = array_filter( $autocomplete($ret), diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 075fe6621cc1f..187bceed2a75f 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -501,6 +501,8 @@ private function createBlock(iterable $messages, string $type = null, string $st } $line = $prefix.$line; + $decorationLength = Helper::strlen($line) - Helper::strlenWithoutDecoration($this->getFormatter(), $line); + $messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength); $line .= str_repeat(' ', max($this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line), 0)); if ($style) { diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php deleted file mode 100644 index 8460e7ececf37..0000000000000 --- a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_21.php +++ /dev/null @@ -1,13 +0,0 @@ -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/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt deleted file mode 100644 index aee3c4a89c2e7..0000000000000 --- a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_21.txt +++ /dev/null @@ -1,7 +0,0 @@ - - [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 πŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎπŸ‘©β€πŸŒΎ - From 903caf6e2a8814a083e207e29b420c55cdfc9447 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 3 Apr 2021 13:37:44 +0200 Subject: [PATCH 2/2] Dont break BC --- src/Symfony/Component/Console/Helper/Helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Helper.php b/src/Symfony/Component/Console/Helper/Helper.php index f82dd286fc3b5..579c8ddb90ba4 100644 --- a/src/Symfony/Component/Console/Helper/Helper.php +++ b/src/Symfony/Component/Console/Helper/Helper.php @@ -116,17 +116,17 @@ 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)); } - 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);