Skip to content

Commit ce3d3f7

Browse files
bug #60042 [Console] Table counts wrong number of padding symbols in renderCell() method when cell contain unicode variant selector (vladimir-vv)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Console] Table counts wrong number of padding symbols in `renderCell()` method when cell contain unicode variant selector | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? |no | Issues | Fix #60038 | License | MIT When rendering a table in console and use emojis then width of columns could be wrong calculated. Commits ------- 380afcc [Console] Table counts wrong number of padding symbols in `renderCell()` method when cell contain unicode variant selector
2 parents 53c18cf + 380afcc commit ce3d3f7

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ private function splitStringByWidth(string $string, int $width): array
12781278

12791279
foreach (preg_split('//u', $m[0]) as $char) {
12801280
// test if $char could be appended to current line
1281-
if (mb_strwidth($line.$char, 'utf8') <= $width) {
1281+
if (Helper::width($line.$char) <= $width) {
12821282
$line .= $char;
12831283
continue;
12841284
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public static function width(?string $string): int
4848
$string ??= '';
4949

5050
if (preg_match('//u', $string)) {
51-
return (new UnicodeString($string))->width(false);
51+
$string = preg_replace('/[\p{Cc}\x7F]++/u', '', $string, -1, $count);
52+
53+
return (new UnicodeString($string))->width(false) + $count;
5254
}
5355

5456
if (false === $encoding = mb_detect_encoding($string, null, true)) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string
564564
}
565565

566566
// str_pad won't work properly with multi-byte strings, we need to fix the padding
567-
if (false !== $encoding = mb_detect_encoding($cell, null, true)) {
568-
$width += \strlen($cell) - mb_strwidth($cell, $encoding);
569-
}
570-
567+
$width += \strlen($cell) - Helper::width($cell) - substr_count($cell, "\0");
571568
$style = $this->getColumnStyle($column);
572569

573570
if ($cell instanceof TableSeparator) {

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,9 +1294,9 @@ public static function renderSetTitle()
12941294
'footer',
12951295
'default',
12961296
<<<'TABLE'
1297-
+---------------+---- Multiline
1297+
+---------------+--- Multiline
12981298
header
1299-
here -+------------------+
1299+
here +------------------+
13001300
| ISBN | Title | Author |
13011301
+---------------+--------------------------+------------------+
13021302
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
@@ -2078,4 +2078,36 @@ public function testGithubIssue52101HorizontalFalse()
20782078
$this->getOutputContent($output)
20792079
);
20802080
}
2081+
2082+
public function testGithubIssue60038WidthOfCellWithEmoji()
2083+
{
2084+
$table = (new Table($output = $this->getOutputStream()))
2085+
->setHeaderTitle('Test Title')
2086+
->setHeaders(['Title', 'Author'])
2087+
->setRows([
2088+
["🎭 💫 ☯"." Divine Comedy", "Dante Alighieri"],
2089+
// the snowflake (e2 9d 84 ef b8 8f) has a variant selector
2090+
["👑 ❄️ 🗡"." Game of Thrones", "George R.R. Martin"],
2091+
// the snowflake in text style (e2 9d 84 ef b8 8e) has a variant selector
2092+
["❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎", ""],
2093+
["And a very long line to show difference in previous lines", ""],
2094+
])
2095+
;
2096+
$table->render();
2097+
2098+
$this->assertSame(<<<TABLE
2099+
+---------------------------------- Test Title -------------+--------------------+
2100+
| Title | Author |
2101+
+-----------------------------------------------------------+--------------------+
2102+
| 🎭 💫 ☯ Divine Comedy | Dante Alighieri |
2103+
| 👑 ❄️ 🗡 Game of Thrones | George R.R. Martin |
2104+
| ❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎ | |
2105+
| And a very long line to show difference in previous lines | |
2106+
+-----------------------------------------------------------+--------------------+
2107+
2108+
TABLE
2109+
,
2110+
$this->getOutputContent($output)
2111+
);
2112+
}
20812113
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy