Browse Source

add fomating options to shell table helper

antograssiot 10 years ago
parent
commit
f7692a4fe3
2 changed files with 163 additions and 10 deletions
  1. 38 5
      src/Shell/Helper/TableHelper.php
  2. 125 5
      tests/TestCase/Shell/Helper/TableHelperTest.php

+ 38 - 5
src/Shell/Helper/TableHelper.php

@@ -22,6 +22,16 @@ use Cake\Console\Helper;
 class TableHelper extends Helper
 class TableHelper extends Helper
 {
 {
     /**
     /**
+     * Default config for this helper.
+     *
+     * @var array
+     */
+    protected $_defaultConfig = [
+        'headers' => true,
+        'rowSeparator' => false,
+        'headerStyle' => 'info',
+    ];
+    /**
      * Calculate the column widths
      * Calculate the column widths
      *
      *
      * @param array $rows The rows on which the columns width will be calculated on.
      * @param array $rows The rows on which the columns width will be calculated on.
@@ -60,15 +70,18 @@ class TableHelper extends Helper
     /**
     /**
      * Output a row.
      * 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.
      * @param array $widths The widths of each column to output.
      * @return void
      * @return void
      */
      */
-    protected function _render($row, $widths)
+    protected function _render($row, $widths, $options = [])
     {
     {
         $out = '';
         $out = '';
         foreach ($row as $i => $column) {
         foreach ($row as $i => $column) {
             $pad = $widths[$i] - mb_strlen($column);
             $pad = $widths[$i] - mb_strlen($column);
+            if (!empty($options['style'])) {
+                $column = $this->_addStyle($column, $options['style']);
+            }
             $out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
             $out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
         }
         }
         $out .= '|';
         $out .= '|';
@@ -83,15 +96,35 @@ class TableHelper extends Helper
      */
      */
     public function output($rows)
     public function output($rows)
     {
     {
+        $config = $this->config();
         $widths = $this->_calculateWidths($rows);
         $widths = $this->_calculateWidths($rows);
 
 
         $this->_rowSeparator($widths);
         $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) {
         foreach ($rows as $line) {
             $this->_render($line, $widths);
             $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 .'>';
     }
     }
 }
 }

+ 125 - 5
tests/TestCase/Shell/Helper/TableHelperTest.php

@@ -42,9 +42,9 @@ class TableHelperTest extends TestCase
     /**
     /**
      * Test output
      * Test output
      *
      *
-     * @return voi
+     * @return void
      */
      */
-    public function testOutput()
+    public function testDefaultOutput()
     {
     {
         $data = [
         $data = [
             ['Header 1', 'Header', 'Long Header'],
             ['Header 1', 'Header', 'Long Header'],
@@ -54,7 +54,7 @@ class TableHelperTest extends TestCase
         $this->helper->output($data);
         $this->helper->output($data);
         $expected = [
         $expected = [
             '+--------------+---------------+---------------+',
             '+--------------+---------------+---------------+',
-            '| Header 1     | Header        | Long Header   |',
+            '| <info>Header 1</info>     | <info>Header</info>        | <info>Long Header</info>   |',
             '+--------------+---------------+---------------+',
             '+--------------+---------------+---------------+',
             '| short        | Longish thing | short         |',
             '| short        | Longish thing | short         |',
             '| Longer thing | short         | Longest Value |',
             '| Longer thing | short         | Longest Value |',
@@ -66,7 +66,7 @@ class TableHelperTest extends TestCase
     /**
     /**
      * Test output with multibyte characters
      * Test output with multibyte characters
      *
      *
-     * @return voi
+     * @return void
      */
      */
     public function testOutputUtf8()
     public function testOutputUtf8()
     {
     {
@@ -78,7 +78,7 @@ class TableHelperTest extends TestCase
         $this->helper->output($data);
         $this->helper->output($data);
         $expected = [
         $expected = [
             '+--------------+-----------+---------------+',
             '+--------------+-----------+---------------+',
-            '| Header 1     | Head      | Long Header   |',
+            '| <info>Header 1</info>     | <info>Head</info>      | <info>Long Header</info>   |',
             '+--------------+-----------+---------------+',
             '+--------------+-----------+---------------+',
             '| short        | ÄÄÄÜÜÜ    | short         |',
             '| short        | ÄÄÄÜÜÜ    | short         |',
             '| Longer thing | longerish | Longest Value |',
             '| Longer thing | longerish | Longest Value |',
@@ -86,4 +86,124 @@ class TableHelperTest extends TestCase
         ];
         ];
         $this->assertEquals($expected, $this->stub->messages());
         $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());
+    }
 }
 }