8000 [Console] Fixed the table rendering with multi-byte strings. · symfony/symfony@ab1439e · GitHub
[go: up one dir, main page]

Skip to content

Commit ab1439e

Browse files
committed
[Console] Fixed the table rendering with multi-byte strings.
1 parent 092fae1 commit ab1439e

File tree

2 files changed

+34
-1
lines changed
  • src/Symfony/Component/Console
    • Helper
    • < 8000 div class="PRIVATE_TreeView-item-container prc-TreeView-TreeViewItemContainer--2Rkn" style="--level:2">
      Tests/Helper

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)) {
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