Skip to content

Commit f7692a4

Browse files
committed
add fomating options to shell table helper
1 parent 61931bf commit f7692a4

File tree

2 files changed

+163
-10
lines changed

2 files changed

+163
-10
lines changed

src/Shell/Helper/TableHelper.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
*/
2222
class TableHelper extends Helper
2323
{
24+
/**
25+
* Default config for this helper.
26+
*
27+
* @var array
28+
*/
29+
protected $_defaultConfig = [
30+
'headers' => true,
31+
'rowSeparator' => false,
32+
'headerStyle' => 'info',
33+
];
2434
/**
2535
* Calculate the column widths
2636
*
@@ -60,15 +70,18 @@ protected function _rowSeparator($widths)
6070
/**
6171
* Output a row.
6272
*
63-
* @param array $row The row to ouptut.
73+
* @param array $row The row to output.
6474
* @param array $widths The widths of each column to output.
6575
* @return void
6676
*/
67-
protected function _render($row, $widths)
77+
protected function _render($row, $widths, $options = [])
6878
{
6979
$out = '';
7080
foreach ($row as $i => $column) {
7181
$pad = $widths[$i] - mb_strlen($column);
82+
if (!empty($options['style'])) {
83+
$column = $this->_addStyle($column, $options['style']);
84+
}
7285
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
7386
}
7487
$out .= '|';
@@ -83,15 +96,35 @@ protected function _render($row, $widths)
8396
*/
8497
public function output($rows)
8598
{
99+
$config = $this->config();
86100
$widths = $this->_calculateWidths($rows);
87101

88102
$this->_rowSeparator($widths);
89-
$this->_render(array_shift($rows), $widths);
90-
$this->_rowSeparator($widths);
103+
if ($config['headers'] === true) {
104+
$this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
105+
$this->_rowSeparator($widths);
106+
}
91107

92108
foreach ($rows as $line) {
93109
$this->_render($line, $widths);
110+
if ($config['rowSeparator'] === true) {
111+
$this->_rowSeparator($widths);
112+
}
94113
}
95-
$this->_rowSeparator($widths);
114+
if ($config['rowSeparator'] !== true) {
115+
$this->_rowSeparator($widths);
116+
}
117+
}
118+
119+
/**
120+
* Add style tags
121+
*
122+
* @param string $text The text to be surrounded
123+
* @param string $style The style to be applied
124+
* @return string
125+
*/
126+
protected function _addStyle($text, $style)
127+
{
128+
return '<' . $style .'>' . $text . '</' . $style .'>';
96129
}
97130
}

tests/TestCase/Shell/Helper/TableHelperTest.php

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function setUp()
4242
/**
4343
* Test output
4444
*
45-
* @return voi
45+
* @return void
4646
*/
47-
public function testOutput()
47+
public function testDefaultOutput()
4848
{
4949
$data = [
5050
['Header 1', 'Header', 'Long Header'],
@@ -54,7 +54,7 @@ public function testOutput()
5454
$this->helper->output($data);
5555
$expected = [
5656
'+--------------+---------------+---------------+',
57-
'| Header 1 | Header | Long Header |',
57+
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
5858
'+--------------+---------------+---------------+',
5959
'| short | Longish thing | short |',
6060
'| Longer thing | short | Longest Value |',
@@ -66,7 +66,7 @@ public function testOutput()
6666
/**
6767
* Test output with multibyte characters
6868
*
69-
* @return voi
69+
* @return void
7070
*/
7171
public function testOutputUtf8()
7272
{
@@ -78,12 +78,132 @@ public function testOutputUtf8()
7878
$this->helper->output($data);
7979
$expected = [
8080
'+--------------+-----------+---------------+',
81-
'| Header 1 | Head | Long Header |',
81+
'| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
8282
'+--------------+-----------+---------------+',
8383
'| short | ÄÄÄÜÜÜ | short |',
8484
'| Longer thing | longerish | Longest Value |',
8585
'+--------------+-----------+---------------+',
8686
];
8787
$this->assertEquals($expected, $this->stub->messages());
8888
}
89+
90+
/**
91+
* Test output without headers
92+
*
93+
* @return void
94+
*/
95+
public function testOutputWithoutHeaderStyle()
96+
{
97+
$data = [
98+
['Header 1', 'Header', 'Long Header'],
99+
['short', 'Longish thing', 'short'],
100+
['Longer thing', 'short', 'Longest Value'],
101+
];
102+
$this->helper->config(['headerStyle' => false]);
103+
$this->helper->output($data);
104+
$expected = [
105+
'+--------------+---------------+---------------+',
106+
'| Header 1 | Header | Long Header |',
107+
'+--------------+---------------+---------------+',
108+
'| short | Longish thing | short |',
109+
'| Longer thing | short | Longest Value |',
110+
'+--------------+---------------+---------------+',
111+
];
112+
$this->assertEquals($expected, $this->stub->messages());
113+
}
114+
/**
115+
* Test output with different header style
116+
*
117+
* @return void
118+
*/
119+
public function testOutputWithDifferentHeaderStyle()
120+
{
121+
$data = [
122+
['Header 1', 'Header', 'Long Header'],
123+
['short', 'Longish thing', 'short'],
124+
['Longer thing', 'short', 'Longest Value'],
125+
];
126+
$this->helper->config(['headerStyle' => 'error']);
127+
$this->helper->output($data);
128+
$expected = [
129+
'+--------------+---------------+---------------+',
130+
'| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
131+
'+--------------+---------------+---------------+',
132+
'| short | Longish thing | short |',
133+
'| Longer thing | short | Longest Value |',
134+
'+--------------+---------------+---------------+',
135+
];
136+
$this->assertEquals($expected, $this->stub->messages());
137+
}
138+
139+
/**
140+
* Test output without table headers
141+
*
142+
* @return void
143+
*/
144+
public function testOutputWithoutHeaders() {
145+
$data = [
146+
['short', 'Longish thing', 'short'],
147+
['Longer thing', 'short', 'Longest Value'],
148+
];
149+
$this->helper->config(['headers' => false]);
150+
$this->helper->output($data);
151+
$expected = [
152+
'+--------------+---------------+---------------+',
153+
'| short | Longish thing | short |',
154+
'| Longer thing | short | Longest Value |',
155+
'+--------------+---------------+---------------+',
156+
];
157+
$this->assertEquals($expected, $this->stub->messages());
158+
}
159+
160+
/**
161+
* Test output with row separator
162+
*
163+
* @return void
164+
*/
165+
public function testOutputWithRowSeparator() {
166+
$data = [
167+
['Header 1', 'Header', 'Long Header'],
168+
['short', 'Longish thing', 'short'],
169+
['Longer thing', 'short', 'Longest Value']
170+
];
171+
$this->helper->config(['rowSeparator' => true]);
172+
$this->helper->output($data);
173+
$expected = [
174+
'+--------------+---------------+---------------+',
175+
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
176+
'+--------------+---------------+---------------+',
177+
'| short | Longish thing | short |',
178+
'+--------------+---------------+---------------+',
179+
'| Longer thing | short | Longest Value |',
180+
'+--------------+---------------+---------------+',
181+
];
182+
$this->assertEquals($expected, $this->stub->messages());
183+
}
184+
185+
/**
186+
* Test output with row separator and no headers
187+
*
188+
* @return void
189+
*/
190+
public function testOutputWithRowSeparatorAndHeaders() {
191+
$data = [
192+
['Header 1', 'Header', 'Long Header'],
193+
['short', 'Longish thing', 'short'],
194+
['Longer thing', 'short', 'Longest Value'],
195+
];
196+
$this->helper->config(['rowSeparator' => true]);
197+
$this->helper->output($data);
198+
$expected = [
199+
'+--------------+---------------+---------------+',
200+
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
201+
'+--------------+---------------+---------------+',
202+
'| short | Longish thing | short |',
203+
'+--------------+---------------+---------------+',
204+
'| Longer thing | short | Longest Value |',
205+
'+--------------+---------------+---------------+',
206+
];
207+
$this->assertEquals($expected, $this->stub->messages());
208+
}
89209
}

0 commit comments

Comments
 (0)