Skip to content

Commit

Permalink
bug #12864 [Console][Table] Fix cell padding with multi-byte (ttsuruoka)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[Console][Table] Fix cell padding with multi-byte

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

When the `TableHelper` dealing with East Asian text, it renders wrong widths. This fixes that problem.

Commits
-------

11014c2 [Console][Table] Fix cell padding with multi-byte
  • Loading branch information
fabpot committed Feb 5, 2015
2 parents 33bf087 + 11014c2 commit bc75c36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Helper.php
Expand Up @@ -41,7 +41,7 @@ public function getHelperSet()
}

/**
* Returns the length of a string, using mb_strlen if it is available.
* Returns the length of a string, using mb_strwidth if it is available.
*
* @param string $string The string to check its length
*
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Helper/TableHelper.php
Expand Up @@ -386,8 +386,8 @@ private function renderCell(array $row, $column, $cellFormat)
$width = $this->getColumnWidth($column);

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

$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);
Expand Down
29 changes: 28 additions & 1 deletion src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php
Expand Up @@ -235,7 +235,7 @@ public function testRenderProvider()

public function testRenderMultiByte()
{
if (!function_exists('mb_strlen')) {
if (!function_exists('mb_strwidth')) {
$this->markTestSkipped('The "mbstring" extension is not available');
}

Expand All @@ -255,6 +255,33 @@ public function testRenderMultiByte()
| 1234 |
+------+
TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
}

public function testRenderFullWidthCharacters()
{
if (!function_exists('mb_strwidth')) {
$this->markTestSkipped('The "mbstring" extension is not available');
}

$table = new TableHelper();
$table
->setHeaders(array('あいうえお'))
->setRows(array(array(1234567890)))
->setLayout(TableHelper::LAYOUT_DEFAULT)
;
$table->render($output = $this->getOutputStream());

$expected =
<<<TABLE
+------------+
| あいうえお |
+------------+
| 1234567890 |
+------------+
TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
Expand Down

0 comments on commit bc75c36

Please sign in to comment.