From c64a53dfc9ecf60f43054d0d8c44fe8b25e844c1 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Mon, 2 Dec 2013 12:36:08 -0500 Subject: [PATCH] extend table helper to have a row divider --- .../Component/Console/Helper/TableHelper.php | 80 ++++++++++++++++++- .../Console/Tests/Helper/TableHelperTest.php | 42 +++++++++- 2 files changed, 116 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/TableHelper.php b/src/Symfony/Component/Console/Helper/TableHelper.php index d6ad0e9e01804..cabcb543600b3 100644 --- a/src/Symfony/Component/Console/Helper/TableHelper.php +++ b/src/Symfony/Component/Console/Helper/TableHelper.php @@ -39,11 +39,20 @@ class TableHelper extends Helper */ private $rows = array(); + /** + * Dividers at row positions. + * + * @var array + */ + private $dividersAt = array(); + // Rendering options private $paddingChar; private $horizontalBorderChar; + private $horizontalDividerChar; private $verticalBorderChar; private $crossingChar; + private $crossingDividerChar; private $cellHeaderFormat; private $cellRowFormat; private $cellRowContentFormat; @@ -88,8 +97,10 @@ public function setLayout($layout) $this ->setPaddingChar(' ') ->setHorizontalBorderChar('=') + ->setHorizontalDividerChar(' ') ->setVerticalBorderChar(' ') ->setCrossingChar(' ') + ->setCrossingDividerChar(' ') ->setCellHeaderFormat('%s') ->setCellRowFormat('%s') ->setCellRowContentFormat(' %s ') @@ -102,8 +113,10 @@ public function setLayout($layout) $this ->setPaddingChar(' ') ->setHorizontalBorderChar('') + ->setHorizontalDividerChar('-') ->setVerticalBorderChar(' ') ->setCrossingChar('') + ->setCrossingDividerChar('-') ->setCellHeaderFormat('%s') ->setCellRowFormat('%s') ->setCellRowContentFormat('%s') @@ -116,8 +129,10 @@ public function setLayout($layout) $this ->setPaddingChar(' ') ->setHorizontalBorderChar('-') + ->setHorizontalDividerChar(' ') ->setVerticalBorderChar('|') ->setCrossingChar('+') + ->setCrossingDividerChar('*') ->setCellHeaderFormat('%s') ->setCellRowFormat('%s') ->setCellRowContentFormat(' %s ') @@ -187,13 +202,18 @@ public function addRow(array $row) return $this; } - public function setRow($column, array $row) + public function setRow($position, array $row) { - $this->rows[$column] = $row; + $this->rows[$position] = $row; return $this; } + public function setDividersAt(array $positions) + { + $this->dividersAt = $positions; + } + /** * Sets padding character, used for cell padding. * @@ -226,6 +246,20 @@ public function setHorizontalBorderChar($horizontalBorderChar) return $this; } + /** + * Sets horizontal divider character. + * + * @param string $horizontalDividerChar + * + * @return TableHelper + */ + public function setHorizontalDividerChar($horizontalDividerChar) + { + $this->horizontalDividerChar = $horizontalDividerChar; + + return $this; + } + /** * Sets vertical border character. * @@ -254,6 +288,20 @@ public function setCrossingChar($crossingChar) return $this; } + /** + * Sets crossing divider character. + * + * @param string $crossingDividerChar + * + * @return TableHelper + */ + public function setCrossingDividerChar($crossingDividerChar) + { + $this->crossingDividerChar = $crossingDividerChar; + + return $this; + } + /** * Sets header cell format. * @@ -347,8 +395,11 @@ public function render(OutputInterface $output) if (!empty($this->headers)) { $this->renderRowSeparator(); } - foreach ($this->rows as $row) { + foreach ($this->rows as $key => $row) { $this->renderRow($row, $this->cellRowFormat); + if (in_array($key, $this->dividersAt)) { + $this->renderDivider(); + } } if (!empty($this->rows)) { $this->renderRowSeparator(); @@ -380,6 +431,29 @@ private function renderRowSeparator() $this->output->writeln(sprintf($this->borderFormat, $markup)); } + /** + * Renders horizontal row divider. + * + * Example: | | | | + */ + private function renderDivider() + { + if (0 === $count = $this->getNumberOfColumns()) { + return; + } + + if (!$this->horizontalDividerChar && !$this->crossingDividerChar) { + return; + } + + $markup = $this->crossingDividerChar; + for ($column = 0; $column < $count; $column++) { + $markup .= str_repeat($this->horizontalDividerChar, $this->getColumnWidth($column)).$this->crossingDividerChar; + } + + $this->output->writeln(sprintf($this->borderFormat, $markup)); + } + /** * Renders vertical column separator. */ diff --git a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php index f3cda0dabf9c1..cd98f4359b496 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php @@ -32,13 +32,14 @@ protected function tearDown() /** * @dataProvider testRenderProvider */ - public function testRender($headers, $rows, $layout, $expected) + public function testRender($headers, $rows, $layout, $dividersAt, $expected) { $table = new TableHelper(); $table ->setHeaders($headers) ->setRows($rows) ->setLayout($layout) + ->setDividersAt($dividersAt) ; $table->render($output = $this->getOutputStream()); @@ -48,13 +49,14 @@ public function testRender($headers, $rows, $layout, $expected) /** * @dataProvider testRenderProvider */ - public function testRenderAddRows($headers, $rows, $layout, $expected) + public function testRenderAddRows($headers, $rows, $layout, $dividersAt, $expected) { $table = new TableHelper(); $table ->setHeaders($headers) ->addRows($rows) ->setLayout($layout) + ->setDividersAt($dividersAt) ; $table->render($output = $this->getOutputStream()); @@ -64,12 +66,13 @@ public function testRenderAddRows($headers, $rows, $layout, $expected) /** * @dataProvider testRenderProvider */ - public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected) + public function testRenderAddRowsOneByOne($headers, $rows, $layout, $dividersAt, $expected) { $table = new TableHelper(); $table ->setHeaders($headers) ->setLayout($layout) + ->setDividersAt($dividersAt) ; foreach ($rows as $row) { $table->addRow($row); @@ -93,6 +96,7 @@ public function testRenderProvider() array('ISBN', 'Title', 'Author'), $books, TableHelper::LAYOUT_DEFAULT, + array(), << array( @@ -223,6 +257,7 @@ public function testRenderProvider() array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), ), TableHelper::LAYOUT_DEFAULT, + array(), <<