Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 650c922

Browse filesBrowse files
committed
Make pretty the box style table
1 parent 0032368 commit 650c922
Copy full SHA for 650c922

File tree

5 files changed

+131
-13
lines changed
Filter options

5 files changed

+131
-13
lines changed

‎UPGRADE-4.1.md

Copy file name to clipboardExpand all lines: UPGRADE-4.1.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.0 to 4.1
22
=======================
33

4+
Console
5+
-------
6+
7+
* Deprecated the `setCrossingChar()` method in favor of the `setCrossingChars()` method in `TableStyle`.
8+
49
HttpFoundation
510
--------------
611

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.x to 5.0
22
=======================
33

4+
Console
5+
-------
6+
7+
* Removed the `setCrossingChar()` method in favor of the `setCrossingChars()` method in `TableStyle`.
8+
49
HttpFoundation
510
--------------
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/Table.php
+23-9Lines 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
*/
@@ -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));
@@ -618,29 +632,29 @@ private static function initStyles()
618632
$borderless
619633
->setHorizontalBorderChar('=')
620634
->setVerticalBorderChar(' ')
621-
->setCrossingChar(' ')
635+
->setCrossingChars(' ')
622636
;
623637

624638
$compact = new TableStyle();
625639
$compact
626640
->setHorizontalBorderChar('')
627641
->setVerticalBorderChar(' ')
628-
->setCrossingChar('')
642+
->setCrossingChars('')
629643
->setCellRowContentFormat('%s')
630644
;
631645

632646
$styleGuide = new TableStyle();
633647
$styleGuide
634648
->setHorizontalBorderChar('-')
635649
->setVerticalBorderChar(' ')
636-
->setCrossingChar(' ')
650+
->setCrossingChars(' ')
637651
->setCellHeaderFormat('%s')
638652
;
639653

640654
$box = (new TableStyle())
641655
->setHorizontalBorderChar('')
642656
->setVerticalBorderChar('')
643-
->setCrossingChar('')
657+
->setCrossingChars('', '', '', '', '', '', '', '', '')
644658
;
645659

646660
return array(

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/TableStyle.php
+94Lines changed: 94 additions & 0 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 ';
@@ -114,9 +123,13 @@ public function getVerticalBorderChar()
114123
* @param string $crossingChar
115124
*
116125
* @return $this
126+
*
127+
* @deprecated since version 4.1, to be removed in 5.0. Use {@link setCrossingChars()} instead.
117128
*/
118129
public function setCrossingChar($crossingChar)
119130
{
131+
@trigger_error(sprintf('%s() is deprecated since version 4.1 and will be removed in 5.0. Use setCrossingChars() instead.', __METHOD__), E_USER_DEPRECATED);
132+
120133
$this->crossingChar = $crossingChar;
121134

122135
return $this;
@@ -132,6 +145,87 @@ public function getCrossingChar()
132145
return $this->crossingChar;
133146
}
134147

148+
/**
149+
* Sets crossing characters.
150+
*
151+
* Example:
152+
* <code>
153+
* 1---------------2-----------------------2------------------3
154+
* | ISBN | Title | Author |
155+
* 8---------------0-----------------------0------------------4
156+
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
157+
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
158+
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
159+
* 7---------------6-----------------------6------------------5
160+
* </code>
161+
*
162+
* @param string $cross Crossing char (see #0 of example)
163+
* @param string|null $topLeft Top left char (see #1 of example), equal to $cross if null
164+
* @param string|null $topMid Top mid char (see #2 of example), equal to $cross if null
165+
* @param string|null $topRight Top right char (see #3 of example), equal to $cross if null
166+
* @param string|null $midRight Mid right char (see #4 of example), equal to $cross if null
167+
* @param string|null $bottomRight Bottom right char (see #5 of example), equal to $cross if null
168+
* @param string|null $bottomMid Bottom mid char (see #6 of example), equal to $cross if null
169+
* @param string|null $bottomLeft Bottom left char (see #7 of example), equal to $cross if null
170+
* @param string|null $midLeft Mid left char (see #8 of example), equal to $cross if null
171+
*
172+
* @return $this
173+
*/
174+
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
175+
{
176+
$this->crossingChar = $cross;
177+
$this->crossingTopLeftChar = $topLeft ?? $cross;
178+
$this->crossingTopMidChar = $topMid ?? $cross;
179+
$this->crossingTopRightChar = $topRight ?? $cross;
180+
$this->crossingMidRightChar = $midRight ?? $cross;
181+
$this->crossingBottomRightChar = $bottomRight ?? $cross;
182+
$this->crossingBottomMidChar = $bottomMid ?? $cross;
183+
$this->crossingBottomLeftChar = $bottomLeft ?? $cross;
184+
$this->crossingMidLeftChar = $midLeft ?? $cross;
185+
186+
return $this;
187+
}
188+
189+
public function getCrossingTopRightChar(): string
190+
{
191+
return $this->crossingTopRightChar;
192+
}
193+
194+
public function getCrossingTopMidChar(): string
195+
{
196+
return $this->crossingTopMidChar;
197+
}
198+
199+
public function getCrossingTopLeftChar(): string
200+
{
201+
return $this->crossingTopLeftChar;
202+
}
203+
204+
public function getCrossingMidRightChar(): string
205+
{
206+
return $this->crossingMidRightChar;
207+
}
208+
209+
public function getCrossingBottomRightChar(): string
210+
{
211+
return $this->crossingBottomRightChar;
212+
}
213+
214+
public function getCrossingBottomMidChar(): string
215+
{
216+
return $this->crossingBottomMidChar;
217+
}
218+
219+
public function getCrossingBottomLeftChar(): string
220+
{
221+
return $this->crossingBottomLeftChar;
222+
}
223+
224+
public function getCrossingMidLeftChar(): string
225+
{
226+
return $this->crossingMidLeftChar;
227+
}
228+
135229
/**
136230
* Sets header cell format.
137231
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/TableTest.php
+4-4Lines 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+
->setCrossingChars('.')
632632
;
633633

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.