Skip to content

Commit a31ec13

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

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

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 && $i === 0) {
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy