Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 528eacd

Browse files
committed
Make pretty the box style table
1 parent b0facfe commit 528eacd

File tree

5 files changed

+141
-15
lines changed

5 files changed

+141
-15
lines changed

UPGRADE-4.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Config
77
* Implementing `ParentNodeDefinitionInterface` without the `getChildNodeDefinitions()` method
88
is deprecated and will be unsupported in 5.0.
99

10+
Console
11+
-------
12+
13+
* Deprecated the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.
14+
1015
EventDispatcher
1116
---------------
1217

UPGRADE-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Config
66

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

9+
Console
10+
-------
11+
12+
* Removed the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.
13+
914
EventDispatcher
1015
---------------
1116

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
* @author Саша Стаменковић <[email protected]>
2222
* @author Abdellatif Ait boudad <[email protected]>
2323
* @author Max Grigorian <[email protected]>
24+
* @author Dany Maillard <[email protected]>
2425
*/
2526
class Table
2627
{
28+
private const SEPARATOR_TOP = 0;
29+
private const SEPARATOR_MID = 1;
30+
private const SEPARATOR_BOTTOM = 2;
31+
2732
/**
2833
* Table headers.
2934
*/
@@ -300,15 +305,15 @@ public function render()
300305
}
301306

302307
if ($isHeader || $isFirstRow) {
303-
$this->renderRowSeparator();
308+
$this->renderRowSeparator($isFirstRow ? self::SEPARATOR_MID : self::SEPARATOR_TOP);
304309
if ($isFirstRow) {
305310
$isFirstRow = false;
306311
}
307312
}
308313

309314
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
310315
}
311-
$this->renderRowSeparator();
316+
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);
312317

313318
$this->cleanup();
314319
}
@@ -318,7 +323,7 @@ public function render()
318323
*
319324
* Example: <code>+-----+-----------+-------+</code>
320325
*/
321-
private function renderRowSeparator()
326+
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
322327
{
323328
if (0 === $count = $this->numberOfColumns) {
324329
return;
@@ -328,9 +333,18 @@ private function renderRowSeparator()
328333
return;
329334
}
330335

331-
$markup = $this->style->getCrossingChar();
336+
if (self::SEPARATOR_MID === $type) {
337+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingMidLeftChar(), $this->style->getCrossingChar(), $this->style->getCrossingMidRightChar());
338+
} elseif (self::SEPARATOR_TOP === $type) {
339+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingTopLeftChar(), $this->style->getCrossingTopMidChar(), $this->style->getCrossingTopRightChar());
340+
} else {
341+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingBottomLeftChar(), $this->style->getCrossingBottomMidChar(), $this->style->getCrossingBottomRightChar());
342+
}
343+
344+
$markup = $leftChar;
332345
for ($column = 0; $column < $count; ++$column) {
333-
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
346+
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]);
347+
$markup .= $column === $count - 1 ? $rightChar : $midChar;
334348
}
335349

336350
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
@@ -628,29 +642,29 @@ private static function initStyles()
628642
$borderless
629643
->setHorizontalBorderChar('=')
630644
->setVerticalBorderChar(' ')
631-
->setCrossingChar(' ')
645+
->setDefaultCrossingChar(' ')
632646
;
633647

634648
$compact = new TableStyle();
635649
$compact
636650
->setHorizontalBorderChar('')
637651
->setVerticalBorderChar(' ')
638-
->setCrossingChar('')
652+
->setDefaultCrossingChar('')
639653
->setCellRowContentFormat('%s')
640654
;
641655

642656
$styleGuide = new TableStyle();
643657
$styleGuide
644658
->setHorizontalBorderChar('-')
645659
->setVerticalBorderChar(' ')
646-
->setCrossingChar(' ')
660+
->setDefaultCrossingChar(' ')
647661
->setCellHeaderFormat('%s')
648662
;
649663

650664
$box = (new TableStyle())
651665
->setHorizontalBorderChar('')
652666
->setVerticalBorderChar('')
653-
->setCrossingChar('')
667+
->setCrossingChars('', '', '', '', '', '', '', '', '')
654668
;
655669

656670
return array(

src/Symfony/Component/Console/Helper/TableStyle.php

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
*
2020
* @author Fabien Potencier <[email protected]>
2121
* @author Саша Стаменковић <[email protected]>
22+
* @author Dany Maillard <[email protected]>
2223
*/
2324
class TableStyle
2425
{
2526
private $paddingChar = ' ';
2627
private $horizontalBorderChar = '-';
2728
private $verticalBorderChar = '|';
2829
private $crossingChar = '+';
30+
private $crossingTopRightChar = '+';
31+
private $crossingTopMidChar = '+';
32+
private $crossingTopLeftChar = '+';
33+
private $crossingMidRightChar = '+';
34+
private $crossingBottomRightChar = '+';
35+
private $crossingBottomMidChar = '+';
36+
private $crossingBottomLeftChar = '+';
37+
private $crossingMidLeftChar = '+';
2938
private $cellHeaderFormat = '<info>%s</info>';
3039
private $cellRowFormat = '%s';
3140
private $cellRowContentFormat = ' %s ';
@@ -108,18 +117,71 @@ public function getVerticalBorderChar()
108117
return $this->verticalBorderChar;
109118
}
110119

120+
/**
121+
* Sets crossing characters.
122+
*
123+
* Example:
124+
* <code>
125+
* 1---------------2-----------------------2------------------3
126+
* | ISBN | Title | Author |
127+
* 8---------------0-----------------------0------------------4
128+
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
129+
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
130+
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
131+
* 7---------------6-----------------------6------------------5
132+
* </code>
133+
*
134+
* @param string $cross Crossing char (see #0 of example)
135+
* @param string $topLeft Top left char (see #1 of example)
136+
* @param string $topMid Top mid char (see #2 of example)
137+
* @param string $topRight Top right char (see #3 of example)
138+
* @param string $midRight Mid right char (see #4 of example)
139+
* @param string $bottomRight Bottom right char (see #5 of example)
140+
* @param string $bottomMid Bottom mid char (see #6 of example)
141+
* @param string $bottomLeft Bottom left char (see #7 of example)
142+
* @param string $midLeft Mid left char (see #8 of example)
143+
*
144+
* @return $this
145+
*/
146+
public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft): self
147+
{
148+
$this->crossingChar = $cross;
149+
$this->crossingTopLeftChar = $topLeft;
150+
$this->crossingTopMidChar = $topMid;
151+
$this->crossingTopRightChar = $topRight;
152+
$this->crossingMidRightChar = $midRight;
153+
$this->crossingBottomRightChar = $bottomRight;
154+
$this->crossingBottomMidChar = $bottomMid;
155+
$this->crossingBottomLeftChar = $bottomLeft;
156+
$this->crossingMidLeftChar = $midLeft;
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* Sets default crossing character used for each cross.
163+
*
164+
* @see {@link setCrossingChars()} for setting each crossing individually.
165+
*/
166+
public function setDefaultCrossingChar(string $char): self
167+
{
168+
return $this->setCrossingChars($char, $char, $char, $char, $char, $char, $char, $char, $char);
169+
}
170+
111171
/**
112172
* Sets crossing character.
113173
*
114174
* @param string $crossingChar
115175
*
116176
* @return $this
177+
*
178+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use {@link setDefaultCrossingChar()} instead.
117179
*/
118180
public function setCrossingChar($crossingChar)
119181
{
120-
$this->crossingChar = $crossingChar;
182+
@trigger_error(sprintf('Method %s() is deprecated since Symfony 4.1 and will be removed in 5.0. Use setDefaultCrossingChar() instead.', __METHOD__), E_USER_DEPRECATED);
121183

122-
return $this;
184+
return $this->setDefaultCrossingChar($crossingChar);
123185
}
124186

125187
/**
@@ -132,6 +194,46 @@ public function getCrossingChar()
132194
return $this->crossingChar;
133195
}
134196

197+
public function getCrossingTopRightChar(): string
198+
{
199+
return $this->crossingTopRightChar;
200+
}
201+
202+
public function getCrossingTopMidChar(): string
203+
{
204+
return $this->crossingTopMidChar;
205+
}
206+
207+
public function getCrossingTopLeftChar(): string
208+
{
209+
return $this->crossingTopLeftChar;
210+
}
211+
212+
public function getCrossingMidRightChar(): string
213+
{
214+
return $this->crossingMidRightChar;
215+
}
216+
217+
public function getCrossingBottomRightChar(): string
218+
{
219+
return $this->crossingBottomRightChar;
220+
}
221+
222+
public function getCrossingBottomMidChar(): string
223+
{
224+
return $this->crossingBottomMidChar;
225+
}
226+
227+
public function getCrossingBottomLeftChar(): string
228+
{
229+
return $this->crossingBottomLeftChar;
230+
}
231+
232+
public function getCrossingMidLeftChar(): string
233+
{
234+
return $this->crossingMidLeftChar;
235+
}
236+
135237
/**
136238
* Sets header cell format.
137239
*

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ public function renderProvider()
143143
$books,
144144
'box',
145145
<<<'TABLE'
146-
───────────────────────────────────────────────────────────
146+
───────────────────────────────────────────────────────────
147147
│ ISBN │ Title │ Author │
148-
───────────────┼──────────────────────────┼──────────────────
148+
───────────────┼──────────────────────────┼──────────────────
149149
│ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri │
150150
│ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens │
151151
│ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien │
152152
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
153-
───────────────────────────────────────────────────────────
153+
───────────────────────────────────────────────────────────
154154

155155
TABLE
156156
),
@@ -628,7 +628,7 @@ public function testStyle()
628628
$style
629629
->setHorizontalBorderChar('.')
630630
->setVerticalBorderChar('.')
631-
->setCrossingChar('.')
631+
->setDefaultCrossingChar('.')
632632
;
633633

634634
Table::setStyleDefinition('dotfull', $style);

0 commit comments

Comments
 (0)