Skip to content

Commit

Permalink
add fomating options to shell table helper
Browse files Browse the repository at this point in the history
  • Loading branch information
antograssiot committed May 31, 2015
1 parent 61931bf commit f7692a4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 10 deletions.
43 changes: 38 additions & 5 deletions src/Shell/Helper/TableHelper.php
Expand Up @@ -21,6 +21,16 @@
*/
class TableHelper extends Helper
{
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [
'headers' => true,
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Calculate the column widths
*
Expand Down Expand Up @@ -60,15 +70,18 @@ protected function _rowSeparator($widths)
/**
* Output a row.
*
* @param array $row The row to ouptut.
* @param array $row The row to output.
* @param array $widths The widths of each column to output.
* @return void
*/
protected function _render($row, $widths)
protected function _render($row, $widths, $options = [])
{
$out = '';
foreach ($row as $i => $column) {
$pad = $widths[$i] - mb_strlen($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
$out .= '|';
Expand All @@ -83,15 +96,35 @@ protected function _render($row, $widths)
*/
public function output($rows)
{
$config = $this->config();
$widths = $this->_calculateWidths($rows);

$this->_rowSeparator($widths);
$this->_render(array_shift($rows), $widths);
$this->_rowSeparator($widths);
if ($config['headers'] === true) {
$this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
$this->_rowSeparator($widths);
}

foreach ($rows as $line) {
$this->_render($line, $widths);
if ($config['rowSeparator'] === true) {
$this->_rowSeparator($widths);
}
}
$this->_rowSeparator($widths);
if ($config['rowSeparator'] !== true) {
$this->_rowSeparator($widths);
}
}

/**
* Add style tags
*
* @param string $text The text to be surrounded
* @param string $style The style to be applied
* @return string
*/
protected function _addStyle($text, $style)
{
return '<' . $style .'>' . $text . '</' . $style .'>';
}
}
130 changes: 125 additions & 5 deletions tests/TestCase/Shell/Helper/TableHelperTest.php
Expand Up @@ -42,9 +42,9 @@ public function setUp()
/**
* Test output
*
* @return voi
* @return void
*/
public function testOutput()
public function testDefaultOutput()
{
$data = [
['Header 1', 'Header', 'Long Header'],
Expand All @@ -54,7 +54,7 @@ public function testOutput()
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| Header 1 | Header | Long Header |',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
Expand All @@ -66,7 +66,7 @@ public function testOutput()
/**
* Test output with multibyte characters
*
* @return voi
* @return void
*/
public function testOutputUtf8()
{
Expand All @@ -78,12 +78,132 @@ public function testOutputUtf8()
$this->helper->output($data);
$expected = [
'+--------------+-----------+---------------+',
'| Header 1 | Head | Long Header |',
'| <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
*
* @return void
*/
public function testOutputWithoutHeaderStyle()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| Header 1 | Header | Long Header |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}
/**
* Test output with different header style
*
* @return void
*/
public function testOutputWithDifferentHeaderStyle()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => 'error']);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output without table headers
*
* @return void
*/
public function testOutputWithoutHeaders() {
$data = [
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headers' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output with row separator
*
* @return void
*/
public function testOutputWithRowSeparator() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value']
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output with row separator and no headers
*
* @return void
*/
public function testOutputWithRowSeparatorAndHeaders() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}
}

0 comments on commit f7692a4

Please sign in to comment.