From 11014c271f29a7e6add1f0bc61d7eb8a5ed9e583 Mon Sep 17 00:00:00 2001 From: Tatsuya Tsuruoka Date: Fri, 5 Dec 2014 17:50:33 +0900 Subject: [PATCH] [Console][Table] Fix cell padding with multi-byte When the `TableHelper` dealing with East Asian text, it renders wrong widths. This fixes that problem. --- .../Component/Console/Helper/Helper.php | 2 +- .../Component/Console/Helper/TableHelper.php | 4 +-- .../Console/Tests/Helper/TableHelperTest.php | 29 ++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Helper.php b/src/Symfony/Component/Console/Helper/Helper.php index ceef972b01..dc42600de8 100644 --- a/src/Symfony/Component/Console/Helper/Helper.php +++ b/src/Symfony/Component/Console/Helper/Helper.php @@ -41,7 +41,7 @@ abstract class Helper implements HelperInterface } /** - * 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 * diff --git a/src/Symfony/Component/Console/Helper/TableHelper.php b/src/Symfony/Component/Console/Helper/TableHelper.php index 2fd6f5e900..3a9fbeeb18 100644 --- a/src/Symfony/Component/Console/Helper/TableHelper.php +++ b/src/Symfony/Component/Console/Helper/TableHelper.php @@ -387,8 +387,8 @@ class TableHelper extends Helper $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); diff --git a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php index 7a9eb40819..cd9e0f469d 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php @@ -235,7 +235,7 @@ TABLE public function testRenderMultiByte() { - if (!function_exists('mb_strlen')) { + if (!function_exists('mb_strwidth')) { $this->markTestSkipped('The "mbstring" extension is not available'); } @@ -255,6 +255,33 @@ TABLE | 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 = + <<assertEquals($expected, $this->getOutputContent($output));