Skip to content

Commit

Permalink
bug #48886 [Console] Fix computing column width containing multibyte …
Browse files Browse the repository at this point in the history
…chars (cay89)

This PR was merged into the 5.4 branch.

Discussion
----------

[Console] Fix computing column width containing multibyte chars

Use mb_str_split instead of str_split because it works badly with multibyte chars on auto width adjustment.

| Q             | A
| ------------- | ---
| Branch?       | 5.4 and above
| Bug fix?      | yes

Console output with str_split:

![image](https://user-images.githubusercontent.com/3186515/210783727-7f67c371-67c0-4005-9914-e345fa9b5569.png)

Console output with mb_str_split:

![image](https://user-images.githubusercontent.com/3186515/210783808-bcd9e754-4831-43a1-86b3-6cbcdd93a3ec.png)

Commits
-------

f32179ee12 [Console] Fix computing column width containing multibyte chars
  • Loading branch information
nicolas-grekas committed Apr 18, 2023
2 parents 3105944 + 79e7e16 commit 9f16d3e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ private function calculateColumnsWidth(iterable $groups)
$textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
$textLength = Helper::width($textContent);
if ($textLength > 0) {
$contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
$contentColumns = mb_str_split($textContent, ceil($textLength / $cell->getColspan()));
foreach ($contentColumns as $position => $content) {
$row[$i + $position] = $content;
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public static function renderProvider()
],
new TableSeparator(),
[
new TableCell('Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil!', ['colspan' => 3]),
new TableCell('Cupìdĭtâte díctá âtquè pôrrò, tèmpórà exercitátìónèm mòdí ânìmí núllà nèmò vèl níhìl!', ['colspan' => 3]),
],
],
'default',
Expand All @@ -333,7 +333,7 @@ public static function renderProvider()
| 9971-5-0210-0 | A Tale of |
| | Two Cities |
+-------------------------------+-------------------------------+-----------------------------+
| Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
| Cupìdĭtâte díctá âtquè pôrrò, tèmpórà exercitátìónèm mòdí ânìmí núllà nèmò vèl níhìl! |
+-------------------------------+-------------------------------+-----------------------------+

TABLE
Expand Down

0 comments on commit 9f16d3e

Please sign in to comment.