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

Skip to content

Commit 09b244a

Browse files
committed
[Console] Added support for definition list
1 parent 545d38a commit 09b244a

File tree

6 files changed

+161
-8
lines changed

6 files changed

+161
-8
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar`
99
* `Application` implements `ResetInterface`
1010
* marked all dispatched event classes as `@final`
11+
* added support form displaying table horizontally
1112

1213
4.3.0
1314
-----

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Table
4949
*/
5050
private $rows = [];
5151

52+
private $horizontal = false;
53+
5254
/**
5355
* Column widths cache.
5456
*/
@@ -322,6 +324,13 @@ public function setFooterTitle(?string $title): self
322324
return $this;
323325
}
324326

327+
public function setHorizontal(bool $horizontal = true): self
328+
{
329+
$this->horizontal = $horizontal;
330+
331+
return $this;
332+
}
333+
325334
/**
326335
* Renders table to output.
327336
*
@@ -337,14 +346,31 @@ public function setFooterTitle(?string $title): self
337346
*/
338347
public function render()
339348
{
340-
$rows = array_merge($this->headers, [$divider = new TableSeparator()], $this->rows);
349+
$divider = new TableSeparator();
350+
if ($this->horizontal) {
351+
$rows = [];
352+
foreach ($this->headers[0] ?? [] as $i => $header) {
353+
$rows[$i] = [$header];
354+
foreach ($this->rows as $row) {
355+
if ($row instanceof TableSeparator) {
356+
continue;
357+
}
358+
if (isset($row[$i])) {
359+
$rows[$i][] = $row[$i];
360+
}
361+
}
362+
}
363+
} else {
364+
$rows = array_merge($this->headers, [$divider], $this->rows);
365+
}
366+
341367
$this->calculateNumberOfColumns($rows);
342368

343369
$rows = $this->buildTableRows($rows);
344370
$this->calculateColumnsWidth($rows);
345371

346-
$isHeader = true;
347-
$isFirstRow = false;
372+
$isHeader = !$this->horizontal;
373+
$isFirstRow = $this->horizontal;
348374
foreach ($rows as $row) {
349375
if ($divider === $row) {
350376
$isHeader = false;
@@ -369,8 +395,11 @@ public function render()
369395
$this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat());
370396
}
371397
}
372-
373-
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
398+
if ($this->horizontal) {
399+
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
400+
} else {
401+
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
402+
}
374403
}
375404
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
376405

@@ -450,13 +479,17 @@ private function renderColumnSeparator(int $type = self::BORDER_OUTSIDE)
450479
*
451480
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
452481
*/
453-
private function renderRow(array $row, string $cellFormat)
482+
private function renderRow(array $row, string $cellFormat, string $firstCellFormat = null)
454483
{
455484
$rowContent = $this->renderColumnSeparator(self::BORDER_OUTSIDE);
456485
$columns = $this->getRowColumns($row);
457486
$last = \count($columns) - 1;
458487
foreach ($columns as $i => $column) {
459-
$rowContent .= $this->renderCell($row, $column, $cellFormat);
488+
if ($firstCellFormat && 0 === $i) {
489+
$rowContent .= $this->renderCell($row, $column, $firstCellFormat);
490+
} else {
491+
$rowContent .= $this->renderCell($row, $column, $cellFormat);
492+
}
460493
$rowContent .= $this->renderColumnSeparator($last === $i ? self::BORDER_OUTSIDE : self::BORDER_INSIDE);
461494
}
462495
$this->output->writeln($rowContent);

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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;
21+
use Symfony\Component\Console\Helper\TableSeparator;
2022
use Symfony\Component\Console\Input\InputInterface;
2123
use Symfony\Component\Console\Output\BufferedOutput;
2224
use Symfony\Component\Console\Output\OutputInterface;
@@ -176,7 +178,7 @@ public function caution($message)
176178
/**
177179
* {@inheritdoc}
178180
*/
179-
public function table(array $headers, array $rows)
181+
public function table(array $headers, array $rows, bool $horizontal = false)
180182
{
181183
$style = clone Table::getStyleDefinition('symfony-style-guide');
182184
$style->setCellHeaderFormat('<info>%s</info>');
@@ -185,6 +187,42 @@ public function table(array $headers, array $rows)
185187
$table->setHeaders($headers);
186188
$table->setRows($rows);
187189
$table->setStyle($style);
190+
$table->setHorizontal($horizontal);
191+
192+
$table->render();
193+
$this->newLine();
194+
}
195+
196+
public function definitionList(...$list)
197+
{
198+
$style = clone Table::getStyleDefinition('symfony-style-guide');
199+
$style->setCellHeaderFormat('<info>%s</info>');
200+
201+
$table = new Table($this);
202+
$headers = [];
203+
$row = [];
204+
foreach ($list as $value) {
205+
if ($value instanceof TableSeparator) {
206+
$headers[] = $value;
207+
$row[] = $value;
208+
continue;
209+
}
210+
if (\is_string($value)) {
211+
$headers[] = new TableCell($value, ['colspan' => 2]);
212+
$row[] = null;
213+
continue;
214+
}
215+
if (!\is_array($value)) {
216+
throw new \InvalidArgumentException('Value should be an array, string or an instance of TableSeparator.');
217+
}
218+
$headers[] = key($value);
219+
$row[] = current($value);
220+
}
221+
222+
$table->setHeaders($headers);
223+
$table->setRows([$row]);
224+
$table->setHorizontal();
225+
$table->setStyle($style);
188226

189227
$table->render();
190228
$this->newLine();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Helper\TableSeparator;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
use Symfony\Component\Console\Style\SymfonyStyle;
7+
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyle($input, $output);
10+
11+
$output->definitionList(
12+
['foo' => 'bar'],
13+
new TableSeparator(),
14+
'this is a title',
15+
new TableSeparator(),
16+
['foo2' => 'bar2']
17+
);
18+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---------- ---------
2+
foo bar
3+
---------- ---------
4+
this is a title
5+
---------- ---------
6+
foo2 bar2
7+
---------- ---------
8+

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,61 @@ public function testBoxedStyleWithColspan()
11251125
$this->assertSame($expected, $this->getOutputContent($output));
11261126
}
11271127

1128+
public function provideRenderHorizontalTests()
1129+
{
1130+
$headers = ['foo', 'bar', 'baz'];
1131+
$rows = [['one', 'two', 'tree'], ['1', '2', '3']];
1132+
$expected = <<<EOTXT
1133+
+-----+------+---+
1134+
| foo | one | 1 |
1135+
| bar | two | 2 |
1136+
| baz | tree | 3 |
1137+
+-----+------+---+
1138+
1139+
EOTXT;
1140+
yield [$headers, $rows, $expected];
1141+
1142+
$headers = ['foo', 'bar', 'baz'];
1143+
$rows = [['one', 'two'], ['1']];
1144+
$expected = <<<EOTXT
1145+
+-----+-----+---+
1146+
| foo | one | 1 |
1147+
| bar | two | |
1148+
| baz | | |
1149+
+-----+-----+---+
1150+
1151+
EOTXT;
1152+
yield [$headers, $rows, $expected];
1153+
1154+
$headers = ['foo', 'bar', 'baz'];
1155+
$rows = [['one', 'two', 'tree'], new TableSeparator(), ['1', '2', '3']];
1156+
$expected = <<<EOTXT
1157+
+-----+------+---+
1158+
| foo | one | 1 |
1159+
| bar | two | 2 |
1160+
| baz | tree | 3 |
1161+
+-----+------+---+
1162+
1163+
EOTXT;
1164+
yield [$headers, $rows, $expected];
1165+
}
1166+
1167+
/**
1168+
* @dataProvider provideRenderHorizontalTests
1169+
*/
1170+
public function testRenderHorizontal(array $headers, array $rows, string $expected)
1171+
{
1172+
$table = new Table($output = $this->getOutputStream());
1173+
$table
1174+
->setHeaders($headers)
1175+
->setRows($rows)
1176+
->setHorizontal()
1177+
;
1178+
$table->render();
1179+
1180+
$this->assertEquals($expected, $this->getOutputContent($output));
1181+
}
1182+
11281183
protected function getOutputStream($decorated = false)
11291184
{
11301185
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, $decorated);

0 commit comments

Comments
 (0)