Skip to content
Permalink
Browse files

Fixed Shell TableHelper when there is a fullwidth character

  • Loading branch information...
slywalker committed Aug 1, 2016
1 parent ef6d47e commit 752c4d52adee96bb79c155b0536fbc3c0974f2aa
Showing with 27 additions and 3 deletions.
  1. +3 −3 src/Shell/Helper/TableHelper.php
  2. +24 −0 tests/TestCase/Shell/Helper/TableHelperTest.php
@@ -32,7 +32,7 @@ class TableHelper extends Helper
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Calculate the column widths
*
@@ -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;
}
@@ -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']);
}
@@ -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
*

0 comments on commit 752c4d5

Please sign in to comment.
You can’t perform that action at this time.