8000 [Console] fix #9325 extend table helper to have a row divider by cordoval · Pull Request #9680 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] fix #9325 extend table helper to have a row divider #9680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions src/Symfony/Component/Console/Helper/TableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,8 +97,10 @@ public function setLayout($layout)
$this
->setPaddingChar(' ')
->setHorizontalBorderChar('=')
->setHorizontalDividerChar(' ')
->setVerticalBorderChar(' ')
->setCrossingChar(' ')
->setCrossingDividerChar(' ')
->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
Expand All @@ -102,8 +113,10 @@ public function setLayout($layout)
$this
->setPaddingChar(' ')
->setHorizontalBorderChar('')
->setHorizontalDividerChar('-')
->setVerticalBorderChar(' ')
->setCrossingChar('')
->setCrossingDividerChar('-')
->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat('%s')
Expand All @@ -116,8 +129,10 @@ public function setLayout($layout)
$this
->setPaddingChar(' ')
->setHorizontalBorderChar('-')
->setHorizontalDividerChar(' ')
->setVerticalBorderChar('|')
->setCrossingChar('+')
->setCrossingDividerChar('*')
->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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- 10000 >output->writeln(sprintf($this->borderFormat, $markup));
}

/**
* Renders vertical column separator.
*/
Expand Down
42 changes: 39 additions & 3 deletions src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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);
Expand All @@ -93,6 +96,7 @@ public function testRenderProvider()
array('ISBN', 'Title', 'Author'),
$books,
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+---------------+--------------------------+------------------+
| ISBN | Title | Author |
Expand All @@ -109,6 +113,7 @@ public function testRenderProvider()
array('ISBN', 'Title', 'Author'),
$books,
TableHelper::LAYOUT_COMPACT,
array(),
<<<TABLE
ISBN Title Author
99921-58-10-7 Divine Comedy Dante Alighieri
Expand All @@ -122,6 +127,7 @@ public function testRenderProvider()
array('ISBN', 'Title', 'Author'),
$books,
TableHelper::LAYOUT_BORDERLESS,
array(),
<<<TABLE
=============== ========================== ==================
ISBN Title Author
Expand All @@ -143,6 +149,7 @@ public function testRenderProvider()
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+---------------+--------------------------+------------------+
| ISBN | Title | |
Expand All @@ -164,6 +171,7 @@ public function testRenderProvider()
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
Expand All @@ -183,6 +191,7 @@ public function testRenderProvider()
array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"),
),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+---------------+----------------------------+-----------------+
| ISBN | Title | Author |
Expand All @@ -197,12 +206,36 @@ public function testRenderProvider()
| | | Tolkien |
+---------------+----------------------------+-----------------+

TABLE
),
array(
array('ISBN', 'Title', 'Author'),
array(
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
array('9971-5-0210-0'),
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
),
TableHelper::LAYOUT_DEFAULT,
array(2),
<<<TABLE
+---------------+--------------------------+------------------+
| ISBN | Title | Author |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
| 9971-5-0210-0 | | |
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
* * * *
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+------------------+

TABLE
),
array(
array('ISBN', 'Title'),
array(),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+------+-------+
| ISBN | Title |
Expand All @@ -214,6 +247,7 @@ public function testRenderProvider()
array(),
array(),
TableHelper::LAYOUT_DEFAULT,
array(),
'',
),
'Cell text with tags used for Output styling' => array(
Expand All @@ -223,6 +257,7 @@ public function testRenderProvider()
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+---------------+----------------------+-----------------+
| ISBN | Title | Author |
Expand All @@ -240,6 +275,7 @@ public function testRenderProvider()
5036 array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
),
TableHelper::LAYOUT_DEFAULT,
array(),
<<<TABLE
+----------------------------------+----------------------+-----------------+
| ISBN | Title | Author |
Expand Down
0