8000 bug #40524 [Console] fix emojis messing up the line width (MarionLeHe… · symfony/symfony@1044c0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1044c0b

Browse files
bug #40524 [Console] fix emojis messing up the line width (MarionLeHerisson)
This PR was submitted for the 5.x branch but it was merged into the 5.2 branch instead. Discussion ---------- [Console] fix emojis messing up the line width | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37904 | License | MIT Description ======== The emojis, because they take as much space as two characters, would cause the console to display too many spaces to complete a line, which made it uneven, as described in the issue. The fix uses the `width` function instead of `strlen`. To answer @ogizanagi's comment, yes it does work with "composed" emojis. Before : ![image](https://user-images.githubusercontent.com/11477247/111832081-9d72b100-88f0-11eb-8eda-65ee480c898d.png) After : ![image](https://user-images.githubusercontent.com/11477247/111832103-a6638280-88f0-11eb-802e-838d97f61c81.png) Other changes ========== Removed two unused lines of code, the value of `$messageLineLength` was never used. Note ==== I'd like to add some tests, but I don't know how since I think this depends on console client width ? Thanks for your reviews 🙏 Commits ------- 36b36dc [Command] fix emojis messing up the line width
2 parents 4c2f77e + 36b36dc commit 1044c0b

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Helper;
1313

1414
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15+
use Symfony\Component\String\UnicodeString;
1516

1617
/**
1718
* Helper is the base class for all helper classes.
@@ -45,7 +46,11 @@ public function getHelperSet()
4546
*/
4647
public static function strlen(?string $string)
4748
{
48-
$string = (string) $string;
49+
$string ?? $string = '';
50+
51+
if (preg_match('//u', $string)) {
52+
return (new UnicodeString($string))->width(false);
53+
}
4954

5055
if (false === $encoding = mb_detect_encoding($string, null, true)) {
5156
return \strlen($string);
@@ -59,9 +64,9 @@ public static function strlen(?string $string)
5964
*
6065
* @return string The string subset
6166
*/
62-
public static function substr(string $string, int $from, int $length = null)
67+
public static function substr(?string $string, int $from, int $length = null)
6368
{
64-
$string = (string) $string;
69+
$string ?? $string = '';
6570

6671
if (false === $encoding = mb_detect_encoding($string, null, true)) {
6772
return substr($string, $from, $length);
@@ -116,17 +121,23 @@ public static function formatMemory(int $memory)
116121
return sprintf('%d B', $memory);
117122
}
118123

119-
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
124+
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
120125
{
121-
return self::strlen(self::removeDecoration($formatter, $string));
126+
$string = self::removeDecoration($formatter, $string);
127+
128+
if (preg_match('//u', $string)) {
129+
return (new UnicodeString($string))->width(true);
130+
}
131+
132+
return self::strlen($string);
122133
}
123134

124-
public static function removeDecoration(OutputFormatterInterface $formatter, $string)
135+
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)
125136
{
126137
$isDecorated = $formatter->isDecorated();
127138
$formatter->setDecorated(false);
128139
// remove <...> formatting
129-
$string = $formatter->format($string);
140+
$string = $formatter->format($string ?? '');
130141
// remove already formatted characters
131142
$string = preg_replace("/\033\[[^m]*m/", '', $string);
132143
$formatter->setDecorated($isDecorated);

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
311311
$remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
312312
$output->write($remainingCharacters);
313313
$fullChoice .= $remainingCharacters;
314-
$i = self::strlen($fullChoice);
314+
$i = (false === $encoding = mb_detect_encoding($fullChoice, null, true)) ? \strlen($fullChoice) : mb_strlen($fullChoice, $encoding);
315315

316316
$matches = array_filter(
317317
$autocomplete($ret),

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,6 @@ private function createBlock(iterable $messages, string $type = null, string $st
501501
}
502502

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

508506
if ($style) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Style\SymfonyStyle;
6+
7+
//Ensure texts with emojis don't make longer lines than expected
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyle($input, $output);
10+
$output->success('Lorem ipsum dolor sit amet');
11+
$output->success('Lorem ipsum dolor sit amet with one emoji 🎉');
12+
$output->success('Lorem ipsum dolor sit amet with so many of them 👩‍🌾👩‍🌾👩‍🌾👩‍🌾👩‍🌾');
13+
};
Lines changed: 7 additions & 0 deletions
62BC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
[OK] Lorem ipsum dolor sit amet
3+
4+
[OK] Lorem ipsum dolor sit amet with one emoji 🎉
5+
6+
[OK] Lorem ipsum dolor sit amet with so many of them 👩‍🌾👩‍🌾👩‍🌾👩‍🌾👩‍🌾
7+

0 commit comments

Comments
 (0)
0