diff --git a/src/Symfony/Component/Console/Helper/TableHelper.php b/src/Symfony/Component/Console/Helper/TableHelper.php index cf18e4913c..c18f29598a 100644 --- a/src/Symfony/Component/Console/Helper/TableHelper.php +++ b/src/Symfony/Component/Console/Helper/TableHelper.php @@ -359,12 +359,18 @@ class TableHelper extends Helper private function renderCell(array $row, $column, $cellFormat) { $cell = isset($row[$column]) ? $row[$column] : ''; + $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); + } $this->output->write(sprintf( $cellFormat, str_pad( $this->paddingChar.$cell.$this->paddingChar, - $this->getColumnWidth($column), + $width, $this->paddingChar, $this->padType ) diff --git a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php index b2aa82b2de..5873a7fda6 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php @@ -174,6 +174,33 @@ TABLE ); } + public function testRenderMultiByte() + { + if (!function_exists('mb_strlen')) { + $this->markTestSkipped('The "mbstring" extension is not available'); + } + + $table = new TableHelper(); + $table + ->setHeaders(array('■■')) + ->setRows(array(array(1234))) + ->setLayout(TableHelper::LAYOUT_DEFAULT) + ; + $table->render($output = $this->getOutputStream()); + + $expected = +<<assertEquals($expected, $this->getOutputContent($output)); + } + protected function getOutputStream() { return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);