8000 [Console] [TableHelper] make it work with SymfonyStyle. · symfony/symfony@43cc93c · GitHub
[go: up one dir, main page]

Skip to content

Commit 43cc93c

Browse files
committed
[Console] [TableHelper] make it work with SymfonyStyle.
1 parent da357d8 commit 43cc93c

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Helper\ProgressBar;
1818
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
1919
use Symfony\Component\Console\Helper\Table;
20+
use Symfony\Component\Console\Helper\TableCell;
2021
use Symfony\Component\Console\Input\InputInterface;
2122
use Symfony\Component\Console\Output\BufferedOutput;
2223
use Symfony\Component\Console\Output\OutputInterface;
@@ -204,7 +205,16 @@ public function caution($message)
204205
*/
205206
public function table(array $headers, array $rows)
206207
{
207-
$headers = array_map(function ($value) { return sprintf('<info>%s</>', $value); }, $headers);
208+
array_walk_recursive($headers, function (&$value) {
209+
if ($value instanceof TableCell) {
210+
$value = new TableCell(sprintf('<info>%s</>', $value), array(
211+
'colspan' => $value->getColspan(),
212+
'rowspan' => $value->getRowspan(),
213+
));
214+
} else {
215+
$value = sprintf('<info>%s</>', $value);
216+
}
217+
});
208218

209219
$table = new Table($this);
210220
$table->setHeaders($headers);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
use Symfony\Component\Console\Helper\TableCell;
7+
8+
//Ensure formatting tables when using multiple headers with TableCell
9+
return function (InputInterface $input, OutputInterface $output) {
10+
$headers = array(
11+
array(new TableCell('Main table title', array('colspan' => 3))),
12+
array('ISBN', 'Title', 'Author'),
13+
);
14+
15+
$rows = array(
16+
array(
17+
'978-0521567817',
18+
'De Monarchia',
19+
new TableCell("Dante Alighieri\nspans multiple rows", array('rowspan' => 2)),
20+
),
21+
array('978-0804169127', 'Divine Comedy'),
22+
);
23+
24+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
25+
$output->table($headers, $rows);
26+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---------------- --------------- ---------------------
2+
Main table title
3+
---------------- --------------- ---------------------
4+
ISBN Title Author
5+
---------------- --------------- ---------------------
6+
978-0521567817 De Monarchia Dante Alighieri
7+
978-0804169127 Divine Comedy spans multiple rows
8+
---------------- --------------- ---------------------
9+

0 commit comments

Comments
 (0)
0