Skip to content

Commit

Permalink
Merge pull request #9196 from slywalker/master
Browse files Browse the repository at this point in the history
Fixed Shell TableHelper when there is a fullwidth character
  • Loading branch information
markstory committed Aug 1, 2016
2 parents ef6d47e + 752c4d5 commit d1bdad7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Shell/Helper/TableHelper.php
Expand Up @@ -32,7 +32,7 @@ class TableHelper extends Helper
'rowSeparator' => false,
'headerStyle' => 'info',
];

/**
* Calculate the column widths
*
Expand All @@ -44,7 +44,7 @@ protected function _calculateWidths($rows)
$widths = [];
foreach ($rows as $line) {
foreach ($line as $k => $v) {
$columnLength = mb_strlen($line[$k]);
$columnLength = mb_strwidth($line[$k]);
if ($columnLength > (isset($widths[$k]) ? $widths[$k] : 0)) {
$widths[$k] = $columnLength;
}
Expand Down Expand Up @@ -86,7 +86,7 @@ protected function _render(array $row, $widths, $options = [])

$out = '';
foreach ($row as $i => $column) {
$pad = $widths[$i] - mb_strlen($column);
$pad = $widths[$i] - mb_strwidth($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Shell/Helper/TableHelperTest.php
Expand Up @@ -87,6 +87,30 @@ public function testOutputUtf8()
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output with multi-byte characters
*
* @return void
*/
public function testOutputFullwidth()
{
$data = [
['Header 1', 'Head', 'Long Header'],
['short', '竜頭蛇尾', 'short'],
['Longer thing', 'longerish', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+-----------+---------------+',
'| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
'+--------------+-----------+---------------+',
'| short | 竜頭蛇尾 | short |',
'| Longer thing | longerish | Longest Value |',
'+--------------+-----------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output without headers
*
Expand Down

0 comments on commit d1bdad7

Please sign in to comment.