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

Skip to content

Commit 045bb41

Browse files
committed
Console Table vertical rendering
1 parent 2a3c003 commit 045bb41

File tree

3 files changed

+427
-2
lines changed

3 files changed

+427
-2
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add support for vertical table rendering
8+
49
5.3
510
---
611

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

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

5354
/**
5455
* Column widths cache.
@@ -318,6 +319,13 @@ public function setHorizontal(bool $horizontal = true): self
318319
return $this;
319320
}
320321

322+
public function setVertical(bool $vertical = true): self
323+
{
324+
$this->vertical = $vertical;
325+
326+
return $this;
327+
}
328+
321329
/**
322330
* Renders table to output.
323331
*
@@ -334,8 +342,12 @@ public function setHorizontal(bool $horizontal = true): self
334342
public function render()
335343
{
336344
$divider = new TableSeparator();
345+
$isCellWithColspan = static function ($cell): bool {
346+
return $cell instanceof TableCell && $cell->getColspan() >= 2;
347+
};
348+
349+
$rows = [];
337350
if ($this->horizontal) {
338-
$rows = [];
339351
foreach ($this->headers[0] ?? [] as $i => $header) {
340352
$rows[$i] = [$header];
341353
foreach ($this->rows as $row) {
@@ -344,13 +356,46 @@ public function render()
344356
}
345357
if (isset($row[$i])) {
346358
$rows[$i][] = $row[$i];
347-
} elseif ($rows[$i][0] instanceof TableCell && $rows[$i][0]->getColspan() >= 2) {
359+
} elseif ($isCellWithColspan($rows[$i][0])) {
348360
// Noop, there is a "title"
349361
} else {
350362
$rows[$i][] = null;
351363
}
352364
}
353365
}
366+
} elseif ($this->vertical) {
367+
$maxHeaderLength = max(array_map(static function (string $header) {
368+
return mb_strlen($header);
369+
}, $this->headers[0] ?? ['']));
370+
371+
foreach ($this->rows as $row) {
372+
if ($row instanceof TableSeparator) {
373+
continue;
374+
}
375+
376+
if (0 < \count($rows)) {
377+
$rows[] = [$divider];
378+
}
379+
380+
$containsColspan = 0 < \count(array_filter($row, static function ($cell) use ($isCellWithColspan): bool {
381+
return $isCellWithColspan($cell);
382+
}));
383+
384+
$headers = $this->headers[0] ?? [];
385+
$maxRows = max(\count($headers), \count($row));
386+
for ($i = 0; $i < $maxRows; ++$i) {
387+
$cell = (string) ($row[$i] ?? '');
388+
if (!empty($headers) && !$containsColspan) {
389+
$rows[] = [sprintf(
390+
'<comment>%s</>: %s',
391+
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
392+
$cell
393+
)];
394+
} elseif (!empty($cell)) {
395+
$rows[] = [$cell];
396+
}
397+
}
398+
}
354399
} else {
355400
$rows = array_merge($this->headers, [$divider], $this->rows);
356401
}
@@ -386,6 +431,11 @@ public function render()
386431
$this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat());
387432
}
388433
}
434+
if ($this->vertical) {
435+
$isHeader = false;
436+
$isFirstRow = false;
437+
}
438+
389439
if ($this->horizontal) {
390440
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
391441
} else {

0 commit comments

Comments
 (0)