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

Skip to content

Commit a3f71c0

Browse files
committed
Make pretty the box style table
1 parent e19a34d commit a3f71c0

File tree

3 files changed

+154
-12
lines changed

3 files changed

+154
-12
lines changed

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

Lines changed: 20 additions & 6 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
*/
@@ -281,7 +286,7 @@ public function render()
281286

282287
$this->calculateColumnsWidth(array_merge($headers, $rows));
283288

284-
$this->renderRowSeparator();
289+
$this->renderRowSeparator(self::SEPARATOR_TOP);
285290
if (!empty($headers)) {
286291
foreach ($headers as $header) {
287292
$this->renderRow($header, $this->style->getCellHeaderFormat());
@@ -296,7 +301,7 @@ public function render()
296301
}
297302
}
298303
if (!empty($rows)) {
299-
$this->renderRowSeparator();
304+
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);
300305
}
301306

302307
$this->cleanup();
@@ -307,7 +312,7 @@ public function render()
307312
*
308313
* Example: <code>+-----+-----------+-------+</code>
309314
*/
310-
private function renderRowSeparator()
315+
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
311316
{
312317
if (0 === $count = $this->numberOfColumns) {
313318
return;
@@ -317,9 +322,18 @@ private function renderRowSeparator()
317322
return;
318323
}
319324

320-
$markup = $this->style->getCrossingChar();
325+
if (self::SEPARATOR_MID === $type) {
326+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingMidLeftChar(), $this->style->getCrossingChar(), $this->style->getCrossingMidRightChar());
327+
} elseif (self::SEPARATOR_TOP === $type) {
328+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingTopLeftChar(), $this->style->getCrossingTopMidChar(), $this->style->getCrossingTopRightChar());
329+
} else {
330+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingBottomLeftChar(), $this->style->getCrossingBottomMidChar(), $this->style->getCrossingBottomRightChar());
331+
}
332+
333+
$markup = $leftChar;
321334
for ($column = 0; $column < $count; ++$column) {
322-
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
335+
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]);
336+
$markup .= $column === $count - 1 ? $rightChar : $midChar;
323337
}
324338

325339
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
@@ -640,7 +654,7 @@ private static function initStyles()
640654
$box = (new TableStyle())
641655
->setHorizontalBorderChar('')
642656
->setVerticalBorderChar('')
643-
->setCrossingChar('')
657+
->setCrossingChars('', '', '', '', '', '', '', '', '')
644658
;
645659

646660
return array(

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

Lines changed: 131 additions & 3 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 ';
@@ -117,9 +126,7 @@ public function getVerticalBorderChar()
117126
*/
118127
public function setCrossingChar($crossingChar)
119128
{
120-
$this->crossingChar = $crossingChar;
121-
122-
return $this;
129+
return $this->setCrossingChars($crossingChar);
123130
}
124131

125132
/**
@@ -132,6 +139,127 @@ public function getCrossingChar()
132139
return $this->crossingChar;
133140
}
134141

142+
/**
143+
* Sets crossing characters.
144+
*
145+
* Example:
146+
* <code>
147+
* 1---------------2-----------------------2------------------3
148+
* | ISBN | Title | Author |
149+
* 8---------------0-----------------------0------------------4
150+
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
151+
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
152+
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
153+
* 7---------------6-----------------------6------------------5
154+
* </code>
155+
*
156+
* @param string $cross Crossing char (see #0 of example)
157+
* @param string|null $topLeft Top left char (see #1 of example), equal to $cross if null
158+
* @param string|null $topMid Top mid char (see #2 of example), equal to $cross if null
159+
* @param string|null $topRight Top right char (see #3 of example), equal to $cross if null
160+
* @param string|null $midRight Mid right char (see #4 of example), equal to $cross if null
161+
* @param string|null $bottomRight Bottom right char (see #5 of example), equal to $cross if null
162+
* @param string|null $bottomMid Bottom mid char (see #6 of example), equal to $cross if null
163+
* @param string|null $bottomLeft Bottom left char (see #7 of example), equal to $cross if null
164+
* @param string|null $midLeft Mid left char (see #8 of example), equal to $cross if null
165+
*
166+
* @return $this
167+
*/
168+
public function setCrossingChars(string $cross, string $topLeft = null, string $topMid = null, string $topRight = null, string $midRight = null, string $bottomRight = null, string $bottomMid = null, string $bottomLeft = null, string $midLeft = null): self
169+
{
170+
$this->crossingChar = $cross;
171+
$this->crossingTopLeftChar = $topLeft ?? $cross;
172+
$this->crossingTopMidChar = $topMid ?? $cross;
173+
$this->crossingTopRightChar = $topRight ?? $cross;
174+
$this->crossingMidRightChar = $midRight ?? $cross;
175+
$this->crossingBottomRightChar = $bottomRight ?? $cross;
176+
$this->crossingBottomMidChar = $bottomMid ?? $cross;
177+
$this->crossingBottomLeftChar = $bottomLeft ?? $cross;
178+
$this->crossingMidLeftChar = $midLeft ?? $cross;
179+
180+
return $this;
181+
}
182+
183+
/**
184+
* Gets crossing top right char.
185+
*
186+
* @return string $crossingTopRightChar
187+
*/
188+
public function getCrossingTopRightChar(): string
189+
{
190+
return $this->crossingTopRightChar;
191+
}
192+
193+
/**
194+
* Gets crossing top mid char.
195+
*
196+
* @return string $crossingTopMidChar
197+
*/
198+
public function getCrossingTopMidChar(): string
199+
{
200+
return $this->crossingTopMidChar;
201+
}
202+
203+
/**
204+
* Gets crossing top left char.
205+
*
206+
* @return string $crossingTopLeftChar
207+
*/
208+
public function getCrossingTopLeftChar(): string
209+
{
210+
return $this->crossingTopLeftChar;
211+
}
212+
213+
/**
214+
* Gets crossing mid right char.
215+
*
216+
* @return string $crossingMidRightChar
217+
*/
218+
public function getCrossingMidRightChar(): string
219+
{
220+
return $this->crossingMidRightChar;
221+
}
222+
223+
/**
224+
* Gets crossing bottom right char.
225+
*
226+
* @return string $crossingBottomRightChar
227+
*/
228+
public function getCrossingBottomRightChar(): string
229+
{
230+
return $this->crossingBottomRightChar;
231+
}
232+
233+
/**
234+
* Gets crossing bottom mid char.
235+
*
236+
* @return string $crossingBottomMidChar
237+
*/
238+
public function getCrossingBottomMidChar(): string
239+
{
240+
return $this->crossingBottomMidChar;
241+
}
242+
243+
/**
244+
* Gets crossing bottom left char.
245+
*
246+
* @return string $crossingBottomLeftChar
247+
*/
248+
public function getCrossingBottomLeftChar(): string
249+
{
250+
return $this->crossingBottomLeftChar;
251+
}
252+
253+
/**
254+
* Gets crossing mid left char.
255+
*
256+
* @return string $crossingMidLeftChar
257+
*/
258+
public function getCrossingMidLeftChar(): string
259+
{
260+
return $this->crossingMidLeftChar;
261+
}
262+
135263
/**
136264
* Sets header cell format.
137265
*

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

Lines changed: 3 additions & 3 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
),

0 commit comments

Comments
 (0)