8000 [Console] Table counts wrong number of padding symbols in `renderCell()` method when cell contain unicode variant selector by vladimir-vv · Pull Request #60042 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Table counts wrong number of padding symbols in renderCell() method when cell contain unicode variant selector #60042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ private function splitStringByWidth(string $string, int $width): array

foreach (preg_split('//u', $m[0]) as $char) {
// test if $char could be appended to current line
if (mb_strwidth($line.$char, 'utf8') <= $width) {
if (Helper::width($line.$char) <= $width) {
$line .= $char;
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public static function width(?string $string): int
$string ??= '';

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

return (new UnicodeString($string))->width(false) + $count;
}

if (false === $encoding = mb_detect_encoding($string, null, true)) {
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string
}

// str_pad won't work properly with multi-byte strings, we need to fix the padding
if (false !== $encoding = mb_detect_encoding($cell, null, true)) {
$width += \strlen($cell) - mb_strwidth($cell, $encoding);
}

$width += \strlen($cell) - Helper::width($cell) - substr_count($cell, "\0");
$style = $this->getColumnStyle($column);

if ($cell instanceof TableSeparator) {
Expand Down
36 changes: 34 additions & 2 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,9 +1294,9 @@ public static function renderSetTitle()
'footer',
'default',
<<<'TABLE'
+---------------+---- Multiline
+---------------+--- Multiline
header
here -+------------------+
here +------------------+
| ISBN | Title | Author |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
Expand Down Expand Up @@ -2078,4 +2078,36 @@ public function testGithubIssue52101HorizontalFalse()
$this->getOutputContent($output)
);
}

public function testGithubIssue60038WidthOfCellWithEmoji()
{
$table = (new Table($output = $this->getOutputStream()))
->setHeaderTitle('Test Title')
->setHeaders(['Title', 'Author'])
->setRows([
["🎭 💫 ☯"." Divine Comedy", "Dante Alighieri"],
// the snowflake (e2 9d 84 ef b8 8f) has a variant selector
["👑 ❄️ 🗡"." Game of Thrones", "George R.R. Martin"],
// the snowflake in text style (e2 9d 84 ef b8 8e) has a variant selector
["❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎", ""],
["And a very long line to show difference in previous lines", ""],
])
;
$table->render();

$this->assertSame(<<<TABLE
+---------------------------------- Test Title -------------+--------------------+
| Title | Author |
+-----------------------------------------------------------+--------------------+
| 🎭 💫 ☯ Divine Comedy | Dante Alighieri |
| 👑 ❄️ 🗡 Game of Thrones | George R.R. Martin |
| ❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎ | |
| And a very long line to show difference in previous lines | |
+-----------------------------------------------------------+--------------------+

TABLE
,
$this->getOutputContent($output)
);
}
}
Loading
0