Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit63f973f

Browse files
committed
Add title table
1 parent782ffe2 commit63f973f

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Helper is the base class for all helper classes.
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Dany Maillard <danymaillard93b@gmail.com>
2021
*/
2122
abstractclass Helperimplements HelperInterface
2223
{
@@ -72,6 +73,18 @@ public static function substr($string, $from, $length = null)
7273
returnmb_substr($string,$from,$length,$encoding);
7374
}
7475

76+
/**
77+
* Replaces text within a portion of a string.
78+
*/
79+
publicstaticfunctionsubstr_replace(string$string,string$replacement,int$start,int$length =null):string
80+
{
81+
if (false ===mb_detect_encoding($string,null,true)) {
82+
returnsubstr_replace($string,$replacement,$start,$length);
83+
}
84+
85+
returnmb_substr($string,0,$start).$replacement.mb_substr($string,$start +$length);
86+
}
87+
7588
publicstaticfunctionformatTime($secs)
7689
{
7790
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
privateconstBORDER_OUTSIDE =0;
3535
privateconstBORDER_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+
publicfunctionsetHeaderTitle(?string$title):self
297+
{
298+
$this->headerTitle =$title;
299+
300+
return$this;
301+
}
302+
303+
publicfunctionsetFooterTitle(?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-
privatefunctionrenderRowSeparator(int$type =self::SEPARATOR_MID)
372+
privatefunctionrenderRowSeparator(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+
publicfunctiongetHeaderTitleFormat():string
436+
{
437+
return$this->headerTitleFormat;
438+
}
439+
440+
publicfunctionsetHeaderTitleFormat(string$format):self
441+
{
442+
$this->headerTitleFormat =$format;
443+
444+
return$this;
445+
}
446+
447+
publicfunctiongetFooterTitleFormat():string
448+
{
449+
return$this->footerTitleFormat;
450+
}
451+
452+
publicfunctionsetFooterTitleFormat(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+
publicfunctiontestSetTitle($headerTitle,$footerTitle,$style,$expected)
981+
{
982+
(newTable($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+
publicfunctionrenderSetTitle()
1000+
{
1001+
returnarray(
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
protectedfunctiongetOutputStream($decorated =false)
9781054
{
9791055
returnnewStreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL,$decorated);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp