8000 merged branch jakzal/bugfix/table-helper-multi-byte (PR #8285) · symfony/symfony@0da84da · GitHub
[go: up one dir, main page]

Skip to content

Commit 0da84da

Browse files
committed
merged branch jakzal/bugfix/table-helper-multi-byte (PR #8285)
This PR was merged into the 2.3 branch. Discussion ---------- [Console] Fixed the table rendering with multi-byte strings. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8284 | License | MIT | Doc PR | - Commits ------- ab1439e [Console] Fixed the table rendering with multi-byte strings.
2 parents 092fae1 + ab1439e commit 0da84da

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Symfony/Component/Console/Helper/TableHelper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,18 @@ private function renderRow(array $row, $cellFormat)
359359
private function renderCell(array $row, $column, $cellFormat)
360360
{
361361
$cell = isset($row[$column]) ? $row[$column] : '';
362+
$width = $this->getColumnWidth($column);
363+
364+
// str_pad won't work properly with multi-byte strings, we need to fix the padding
365+
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($cell) 8000 ) {
366+
$width += strlen($cell) - mb_strlen($cell, $encoding);
367+
}
362368

363369
$this->output->write(sprintf(
364370
$cellFormat,
365371
str_pad(
366372
$this->paddingChar.$cell.$this->paddingChar,
367-
$this->getColumnWidth($column),
373+
$width,
368374
$this->paddingChar,
369375
$this->padType
370376
)

src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,33 @@ public function testRenderProvider()
174174
);
175175
}
176176

177+
public function testRenderMultiByte()
178+
{
179+
if (!function_exists('mb_strlen')) {
180+
$this->markTestSkipped('The "mbstring" extension is not available');
181+
}
182+
183+
$table = new TableHelper();
184+
$table
185+
->setHeaders(array('■■'))
186+
->setRows(array(array(1234)))
187+
->setLayout(TableHelper::LAYOUT_DEFAULT)
188+
;
189+
$table->render($output = $this->getOutputStream());
190+
191+
$expected =
192+
<<<TABLE
193+
+------+
194+
| ■■ |
195+
+------+
196+
| 1234 |
197+
+------+
198+
199+
TABLE;
200+
201+
$this->assertEquals($expected, $this->getOutputContent($output));
202+
}
203+
177204
protected function getOutputStream()
178205
{
179206
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);

0 commit comments

Comments
 (0)
0