Skip to content

[Console] Make pretty the box style table #25456

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

Merged
merged 1 commit into from
Mar 17, 2018
Merged
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Config
* Implementing `ParentNodeDefinitionInterface` without the `getChildNodeDefinitions()` method
is deprecated and will be unsupported in 5.0.

Console
-------

* Deprecated the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.

EventDispatcher
---------------

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Config

* Added the `getChildNodeDefinitions()` method to `ParentNodeDefinitionInterface`.

Console
-------

* Removed the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.

EventDispatcher
---------------

Expand Down
33 changes: 24 additions & 9 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
* @author Саша Стаменковић <umpirsky@gmail.com>
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
* @author Max Grigorian <maxakawizard@gmail.com>
* @author Dany Maillard <danymaillard93b@gmail.com>
*/
class Table
{
private const SEPARATOR_TOP = 0;
private const SEPARATOR_MID = 1;
private const SEPARATOR_BOTTOM = 2;

/**
* Table headers.
*/
Expand Down Expand Up @@ -300,15 +305,15 @@ public function render()
}

if ($isHeader || $isFirstRow) {
$this->renderRowSeparator();
$this->renderRowSeparator($isFirstRow ? self::SEPARATOR_MID : self::SEPARATOR_TOP);
if ($isFirstRow) {
$isFirstRow = false;
}
}

$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
}
$this->renderRowSeparator();
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);

$this->cleanup();
}
Expand All @@ -318,7 +323,7 @@ public function render()
*
* Example: <code>+-----+-----------+-------+</code>
*/
private function renderRowSeparator()
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
{
if (0 === $count = $this->numberOfColumns) {
return;
Expand All @@ -328,9 +333,19 @@ private function renderRowSeparator()
return;
}

$markup = $this->style->getCrossingChar();
$chars = $this->style->getCrossingChars();
if (self::SEPARATOR_MID === $type) {
list($leftChar, $midChar, $rightChar) = array($chars[8], $chars[0], $chars[4]);
} elseif (self::SEPARATOR_TOP === $type) {
list($leftChar, $midChar, $rightChar) = array($chars[1], $chars[2], $chars[3]);
} else {
list($leftChar, $midChar, $rightChar) = array($chars[7], $chars[6], $chars[5]);
}

$markup = $leftChar;
for ($column = 0; $column < $count; ++$column) {
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]);
$markup .= $column === $count - 1 ? $rightChar : $midChar;
}

$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
Expand Down Expand Up @@ -628,29 +643,29 @@ private static function initStyles()
$borderless
->setHorizontalBorderChar('=')
->setVerticalBorderChar(' ')
->setCrossingChar(' ')
->setDefaultCrossingChar(' ')
;

$compact = new TableStyle();
$compact
->setHorizontalBorderChar('')
->setVerticalBorderChar(' ')
->setCrossingChar('')
->setDefaultCrossingChar('')
->setCellRowContentFormat('%s')
;

$styleGuide = new TableStyle();
$styleGuide
->setHorizontalBorderChar('-')
->setVerticalBorderChar(' ')
->setCrossingChar(' ')
->setDefaultCrossingChar(' ')
->setCellHeaderFormat('%s')
;

$box = (new TableStyle())
->setHorizontalBorderChar('─')
->setVerticalBorderChar('│')
->setCrossingChar('┼')
->setCrossingChars('┼', '┌', '┬', '┐', '┤', '┘', '┴', '└', '├')
;

return array(
Expand Down
84 changes: 82 additions & 2 deletions src/Symfony/Component/Console/Helper/TableStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Саша Стаменковић <umpirsky@gmail.com>
* @author Dany Maillard <danymaillard93b@gmail.com>
*/
class TableStyle
{
private $paddingChar = ' ';
private $horizontalBorderChar = '-';
private $verticalBorderChar = '|';
private $crossingChar = '+';
private $crossingTopRightChar = '+';
private $crossingTopMidChar = '+';
private $crossingTopLeftChar = '+';
private $crossingMidRightChar = '+';
private $crossingBottomRightChar = '+';
private $crossingBottomMidChar = '+';
private $crossingBottomLeftChar = '+';
private $crossingMidLeftChar = '+';
private $cellHeaderFormat = '<info>%s</info>';
private $cellRowFormat = '%s';
private $cellRowContentFormat = ' %s ';
Expand Down Expand Up @@ -108,18 +117,69 @@ public function getVerticalBorderChar()
return $this->verticalBorderChar;
}

/**
* Sets crossing characters.
*
* Example:
* <code>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using HTML in desc, so this should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<code> tag is sometimes used yes (on master, 17 occurrences). This code-description can be useful IMO.

* 1---------------2-----------------------2------------------3
* | ISBN | Title | Author |
* 8---------------0-----------------------0------------------4
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
* 7---------------6-----------------------6------------------5
* </code>
*
* @param string $cross Crossing char (see #0 of example)
* @param string $topLeft Top left char (see #1 of example)
* @param string $topMid Top mid char (see #2 of example)
* @param string $topRight Top right char (see #3 of example)
* @param string $midRight Mid right char (see #4 of example)
* @param string $bottomRight Bottom right char (see #5 of example)
* @param string $bottomMid Bottom mid char (see #6 of example)
* @param string $bottomLeft Bottom left char (see #7 of example)
* @param string $midLeft Mid left char (see #8 of example)
*/
public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft): self
{
$this->crossingChar = $cross;
$this->crossingTopLeftChar = $topLeft;
$this->crossingTopMidChar = $topMid;
$this->crossingTopRightChar = $topRight;
$this->crossingMidRightChar = $midRight;
$this->crossingBottomRightChar = $bottomRight;
$this->crossingBottomMidChar = $bottomMid;
$this->crossingBottomLeftChar = $bottomLeft;
$this->crossingMidLeftChar = $midLeft;

return $this;
}

/**
* Sets default crossing character used for each cross.
*
* @see {@link setCrossingChars()} for setting each crossing individually.
*/
public function setDefaultCrossingChar(string $char): self
{
return $this->setCrossingChars($char, $char, $char, $char, $char, $char, $char, $char, $char);
}

/**
* Sets crossing character.
*
* @param string $crossingChar
*
* @return $this
*
* @deprecated since Symfony 4.1. Use {@link setDefaultCrossingChar()} instead.
*/
public function setCrossingChar($crossingChar)
{
$this->crossingChar = $crossingChar;
@trigger_error(sprintf('Method %s() is deprecated since Symfony 4.1. Use setDefaultCrossingChar() instead.', __METHOD__), E_USER_DEPRECATED);

return $this;
return $this->setDefaultCrossingChar($crossingChar);
}

/**
Expand All @@ -132,6 +192,26 @@ public function getCrossingChar()
return $this->crossingChar;
}

/**
* Gets crossing characters.
*
* @internal
*/
public function getCrossingChars(): array
{
return array(
$this->crossingChar,
$this->crossingTopLeftChar,
$this->crossingTopMidChar,
$this->crossingTopRightChar,
$this->crossingMidRightChar,
$this->crossingBottomRightChar,
$this->crossingBottomMidChar,
$this->crossingBottomLeftChar,
$this->crossingMidLeftChar,
);
}

/**
* Sets header cell format.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ public function renderProvider()
$books,
'box',
<<<'TABLE'
───────────────────────────────────────────────────────────
───────────────────────────────────────────────────────────
│ ISBN │ Title │ Author │
───────────────┼──────────────────────────┼──────────────────
───────────────┼──────────────────────────┼──────────────────
│ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri │
│ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens │
│ 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
),
Expand Down Expand Up @@ -628,7 +628,7 @@ public function testStyle()
$style
->setHorizontalBorderChar('.')
->setVerticalBorderChar('.')
->setCrossingChar('.')
->setDefaultCrossingChar('.')
;

Table::setStyleDefinition('dotfull', $style);
Expand Down