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

Skip to content

Commit 57da8ae

Browse files
committed
Add title table
1 parent 782ffe2 commit 57da8ae

File tree

4 files changed

+147
-3
lines changed

4 files changed

+147
-3
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ public static function substr($string, $from, $length = null)
7272
return mb_substr($string, $from, $length, $encoding);
7373
}
7474

75+
/**
76+
* Replaces text within a portion of a string.
77+
*/
78+
public static function substr_replace(string $string, string $replacement, int $start, int $length = null): string
79+
{
80+
if (false === mb_detect_encoding($string, null, true)) {
81+
return substr_replace($string, $replacement, $start, $length);
82+
}
83+
84+
return mb_substr($string, 0, $start).$replacement.mb_substr($string, $start + $length);
85+
}
86+
7587
public static function formatTime($secs)
7688
{
7789
static $timeFormats = array(

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class Table
3434
private const BORDER_OUTSIDE = 0;
3535
private const BORDER_INSIDE = 1;
3636

37+
private $headerTitle;
38+
private $footerTitle;
39+
3740
/**
3841
* Table headers.
3942
*/
@@ -290,6 +293,20 @@ public function setRow($column, array $row)
290293
return $this;
291294
}
292295

296+
public function setHeaderTitle(?string $title): self
297+
{
298+
$this->headerTitle = $title;
299+
300+
return $this;
301+
}
302+
303+
public function setfooterTitle(?string $title): self
304+
{
305+
$this->footerTitle = $title;
306+
307+
return $this;
308+
}
309+
293310
/**
294311
* Renders table to output.
295312
*
@@ -331,15 +348,17 @@ public function render()
331348
}
332349

333350
if ($isHeader || $isFirstRow) {
334-
$this->renderRowSeparator($isFirstRow ? self::SEPARATOR_TOP_BOTTOM : self::SEPARATOR_TOP);
335351
if ($isFirstRow) {
352+
$this->renderRowSeparator(self::SEPARATOR_TOP_BOTTOM);
336353
$isFirstRow = false;
354+
} else {
355+
$this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat());
337356
}
338357
}
339358

340359
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
341360
}
342-
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);
361+
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
343362

344363
$this->cleanup();
345364
$this->rendered = true;
@@ -350,7 +369,7 @@ public function render()
350369
*
351370
* Example: <code>+-----+-----------+-------+</code>
352371
*/
353-
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
372+
private function renderRowSeparator(int $type = self::SEPARATOR_MID, string $title = null, string $titleFormat = null)
354373
{
355374
if (0 === $count = $this->numberOfColumns) {
356375
return;
@@ -378,6 +397,17 @@ private function renderRowSeparator(int $type = self::SEPARATOR_MID)
378397
$markup .= $column === $count - 1 ? $rightChar : $midChar;
379398
}
380399

400+
if (null !== $title) {
401+
$titleLength = Helper::strlenWithoutDecoration($formatter = $this->output->getFormatter(), $formattedTitle = sprintf($titleFormat, $title));
402+
$markupLength = Helper::strlen($markup);
403+
if ($titleLength > $limit = $markupLength - 4) {
404+
$titleLength = $limit;
405+
$formatLength = Helper::strlenWithoutDecoration($formatter, sprintf($titleFormat, ''));
406+
$formattedTitle = sprintf($titleFormat, Helper::substr($title, 0, $limit - $formatLength - 3).'...');
407+
}
408+
$markup = Helper::substr_replace($markup, $formattedTitle, ($markupLength - $titleLength) / 2, $titleLength);
409+
}
410+
381411
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
382412
}
383413

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class TableStyle
4040
private $crossingTopLeftBottomChar = '+';
4141
private $crossingTopMidBottomChar = '+';
4242
private $crossingTopRightBottomChar = '+';
43+
private $headerTitleFormat = '<fg=black;bg=white;options=bold> %s </>';
44+
private $footerTitleFormat = '<fg=black;bg=white;options=bold> %s </>';
4345
private $cellHeaderFormat = '<info>%s</info>';
4446
private $cellRowFormat = '%s';
4547
private $cellRowContentFormat = ' %s ';
@@ -429,4 +431,28 @@ public function getPadType()
429431
{
430432
return $this->padType;
431433
}
434+
435+
public function getHeaderTitleFormat(): string
436+
{
437+
return $this->headerTitleFormat;
438+
}
439+
440+
public function setHeaderTitleFormat(string $format): self
441+
{
442+
$this->headerTitleFormat = $format;
443+
444+
return $this;
445+
}
446+
447+
public function getFooterTitleFormat(): string
448+
{
449+
return $this->footerTitleFormat;
450+
}
451+
452+
public function setFooterTitleFormat(string $format): self
453+
{
454+
$this->footerTitleFormat = $format;
455+
456+
return $this;
457+
}
432458
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,82 @@ public function testGetStyleDefinition()
974974
Table::getStyleDefinition('absent');
975975
}
976976

977+
/**
978+
* @dataProvider renderSetTitle
979+
*/
980+
public function testSetTitle($headerTitle, $footerTitle, $style, $expected)
981+
{
982+
(new Table($output = $this->getOutputStream()))
983+
->setHeaderTitle($headerTitle)
984+
->setfooterTitle($footerTitle)
985+
->setHeaders(array('ISBN', 'Title', 'Author'))
986+
->setRows(array(
987+
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
988+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
989+
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
990+
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
991+
))
992+
->setStyle($style)
993+
->render()
994+
;
995+
996+
$this->assertEquals($expected, $this->getOutputContent($output));
997+
}
998+
999+
public function renderSetTitle()
1000+
{
1001+
return array(
1002+
array(
1003+
'Books',
1004+
'Page 1/2',
1005+
'default',
1006+
<<<'TABLE'
1007+
+---------------+----------- Books --------+------------------+
1008+
| ISBN | Title | Author |
1009+
+---------------+--------------------------+------------------+
1010+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
1011+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
1012+
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
1013+
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
1014+
+---------------+--------- Page 1/2 -------+------------------+
1015+
1016+
TABLE
1017+
),
1018+
array(
1019+
'Books',
1020+
'Page 1/2',
1021+
'box',
1022+
<<<'TABLE'
1023+
┌───────────────┬─────────── Books ────────┬──────────────────┐
1024+
│ ISBN │ Title │ Author │
1025+
├───────────────┼──────────────────────────┼──────────────────┤
1026+
│ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri │
1027+
│ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens │
1028+
│ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien │
1029+
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
1030+
└───────────────┴───────── Page 1/2 ───────┴──────────────────┘
1031+
1032+
TABLE
1033+
),
1034+
array(
1035+
'Boooooooooooooooooooooooooooooooooooooooooooooooooooooooks',
1036+
'Page 1/999999999999999999999999999999999999999999999999999',
1037+
'default',
1038+
<<<'TABLE'
1039+
+- Booooooooooooooooooooooooooooooooooooooooooooooooooooo... -+
1040+
| ISBN | Title | Author |
1041+
+---------------+--------------------------+------------------+
1042+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
1043+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
1044+
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
1045+
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
1046+
+- Page 1/99999999999999999999999999999999999999999999999... -+
1047+
1048+
TABLE
1049+
),
1050+
);
1051+
}
1052+
9771053
protected function getOutputStream($decorated = false)
9781054
{
9791055
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, $decorated);

0 commit comments

Comments
 (0)