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

Skip to content

Commit 332f318

Browse files
committed
[Console] Added support for definition list
1 parent 9a5003e commit 332f318

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added `Question::setTrimmable` default to true to allow the answer to be trimmed
88
* added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar`
99
* `Application` implements `ResetInterface`
10+
* added support form displaying table horizontally
1011

1112
4.3.0
1213
-----

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

Lines changed: 35 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
*/
@@ -325,6 +327,13 @@ public function setFooterTitle(?string $title): self
325327
return $this;
326328
}
327329

330+
public function setHorizontal(bool $horizontal = true): self
331+
{
332+
$this->horizontal = $horizontal;
333+
334+
return $this;
335+
}
336+
328337
/**
329338
* Renders table to output.
330339
*
@@ -340,14 +349,26 @@ public function setFooterTitle(?string $title): self
340349
*/
341350
public function render()
342351
{
343-
$rows = array_merge($this->headers, [$divider = new TableSeparator()], $this->rows);
352+
$divider = new TableSeparator();
353+
if ($this->horizontal) {
354+
$rows = [];
355+
foreach ($this->headers[0] as $i => $header) {
356+
$rows[$i] = [$header];
357+
foreach ($this->rows as $row) {
358+
$rows[$i][] = $row[$i];
359+
}
360+
}
361+
} else {
362+
$rows = array_merge($this->headers, [$divider], $this->rows);
363+
}
364+
344365
$this->calculateNumberOfColumns($rows);
345366

346367
$rows = $this->buildTableRows($rows);
347368
$this->calculateColumnsWidth($rows);
348369

349-
$isHeader = true;
350-
$isFirstRow = false;
370+
$isHeader = !$this->horizontal;
371+
$isFirstRow = $this->horizontal;
351372
foreach ($rows as $row) {
352373
if ($divider === $row) {
353374
$isHeader = false;
@@ -372,8 +393,11 @@ public function render()
372393
$this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat());
373394
}
374395
}
375-
376-
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
396+
if ($this->horizontal) {
397+
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
398+
} else {
399+
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
400+
}
377401
}
378402
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
379403

@@ -453,13 +477,17 @@ private function renderColumnSeparator($type = self::BORDER_OUTSIDE)
453477
*
454478
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
455479
*/
456-
private function renderRow(array $row, string $cellFormat)
480+
private function renderRow(array $row, string $cellFormat, string $firstCellFormat = null)
457481
{
458482
$rowContent = $this->renderColumnSeparator(self::BORDER_OUTSIDE);
459483
$columns = $this->getRowColumns($row);
460484
$last = \count($columns) - 1;
461485
foreach ($columns as $i => $column) {
462-
$rowContent .= $this->renderCell($row, $column, $cellFormat);
486+
if ($firstCellFormat && 0 === $i) {
487+
$rowContent .= $this->renderCell($row, $column, $firstCellFormat);
488+
} else {
489+
$rowContent .= $this->renderCell($row, $column, $cellFormat);
490+
}
463491
$rowContent .= $this->renderColumnSeparator($last === $i ? self::BORDER_OUTSIDE : self::BORDER_INSIDE);
464492
}
465493
$this->output->writeln($rowContent);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function caution($message)
176176
/**
177177
* {@inheritdoc}
178178
*/
179-
public function table(array $headers, array $rows)
179+
public function table(array $headers, array $rows, bool $horizontal = false)
180180
{
181181
$style = clone Table::getStyleDefinition('symfony-style-guide');
182182
$style->setCellHeaderFormat('<info>%s</info>');
@@ -185,6 +185,22 @@ public function table(array $headers, array $rows)
185185
$table->setHeaders($headers);
186186
$table->setRows($rows);
187187
$table->setStyle($style);
188+
$table->setHorizontal($horizontal);
189+
190+
$table->render();
191+
$this->newLine();
192+
}
193+
194+
public function definitionList(array $list)
195+
{
196+
$style = clone Table::getStyleDefinition('symfony-style-guide');
197+
$style->setCellHeaderFormat('<info>%s</info>');
198+
199+
$table = new Table($this);
200+
$table->setHeaders(array_keys($list));
201+
$table->setRows([array_values($list)]);
202+
$table->setHorizontal();
203+
$table->setStyle($style);
188204

189205
$table->render();
190206
$this->newLine();

0 commit comments

Comments
 (0)