Skip to content

Commit 59f8464

Browse files
committed
Make pretty the box style table
1 parent b0facfe commit 59f8464

File tree

5 files changed

+139
-15
lines changed

5 files changed

+139
-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 Саша Стаменковић <umpirsky@gmail.com>
2222
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
2323
* @author Max Grigorian <maxakawizard@gmail.com>
24+
* @author Dany Maillard <danymaillard93b@gmail.com>
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: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
*
2020
* @author Fabien Potencier <fabien@symfony.com>
2121
* @author Саша Стаменковић <umpirsky@gmail.com>
22+
* @author Dany Maillard <danymaillard93b@gmail.com>
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,69 @@ 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+
public function setCrossingChars(string $cross, string $topLeft, string $topMid, string $topRight, string $midRight, string $bottomRight, string $bottomMid, string $bottomLeft, string $midLeft): self
145+
{
146+
$this->crossingChar = $cross;
147+
$this->crossingTopLeftChar = $topLeft;
148+
$this->crossingTopMidChar = $topMid;
149+
$this->crossingTopRightChar = $topRight;
150+
$this->crossingMidRightChar = $midRight;
151+
$this->crossingBottomRightChar = $bottomRight;
152+
$this->crossingBottomMidChar = $bottomMid;
153+
$this->crossingBottomLeftChar = $bottomLeft;
154+
$this->crossingMidLeftChar = $midLeft;
155+
156+
return $this;
157+
}
158+
159+
/**
160+
* Sets default crossing character used for each cross.
161+
*
162+
* @see {@link setCrossingChars()} for setting each crossing individually.
163+
*/
164+
public function setDefaultCrossingChar(string $char): self
165+
{
166+
return $this->setCrossingChars($char, $char, $char, $char, $char, $char, $char, $char, $char);
167+
}
168+
111169
/**
112170
* Sets crossing character.
113171
*
114172
* @param string $crossingChar
115173
*
116174
* @return $this
175+
*
176+
* @deprecated since Symfony 4.1, to be removed in 5.0. Use {@link setDefaultCrossingChar()} instead.
117177
*/
118178
public function setCrossingChar($crossingChar)
119179
{
120-
$this->crossingChar = $crossingChar;
180+
@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);
121181

122-
return $this;
182+
return $this->setDefaultCrossingChar($crossingChar);
123183
}
124184

125185
/**
@@ -132,6 +192,46 @@ public function getCrossingChar()
132192
return $this->crossingChar;
133193
}
134194

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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy