8000 [Console] Table vertical rendering by yoannrenard · Pull Request #41676 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Table vertical rendering #41676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Console Table vertical rendering
Using an enum instead of 2 separated booleans

Rephrase changelog for better consistency
  • Loading branch information
yoannrenard committed May 3, 2022
commit 15fa4c5406964778eb89e2faed449807abcf094a
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.1
---

* Add support to display table vertically when calling setVertical()
* Add method `__toString()` to `InputInterface`
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead
* Add suggested values for arguments and options in input definition, for input completion
Expand Down
68 changes: 60 additions & 8 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class Table
private const SEPARATOR_BOTTOM = 3;
private const BORDER_OUTSIDE = 0;
private const BORDER_INSIDE = 1;
private const DISPLAY_ORIENTATION_DEFAULT = 'default';
private const DISPLAY_ORIENTATION_HORIZONTAL = 'horizontal';
private const DISPLAY_ORIENTATION_VERTICAL = 'vertical';

private ?string $headerTitle = null;
private ?string $footerTitle = null;
private array $headers = [];
private array $rows = [];
private bool $horizontal = false;
private array $effectiveColumnWidths = [];
private int $numberOfColumns;
private OutputInterface $output;
Expand All @@ -49,6 +51,7 @@ class Table
private array $columnWidths = [];
private array $columnMaxWidths = [];
private bool $rendered = false;
private string $displayOrientation = self::DISPLAY_ORIENTATION_DEFAULT;

private static array $styles;

Expand Down Expand Up @@ -277,7 +280,17 @@ public function setFooterTitle(?string $title): static
*/
public function setHorizontal(bool $horizontal = true): static
{
$this->horizontal = $horizontal;
$this->displayOrientation = $horizontal ? self::DISPLAY_ORIENTATION_HORIZONTAL : self::DISPLAY_ORIENTATION_DEFAULT;

return $this;
}

/**
* @return $this
*/
public function setVertical(bool $vertical = true): static
{
$this->displayOrientation = $vertical ? self::DISPLAY_ORIENTATION_VERTICAL : self::DISPLAY_ORIENTATION_DEFAULT;

return $this;
}
Expand All @@ -298,8 +311,13 @@ public function setHorizontal(bool $horizontal = true): static
public function render()
{
$divider = new TableSeparator();
if ($this->horizontal) {
$rows = [];
$isCellWithColspan = static fn ($cell): bool => $cell instanceof TableCell && $cell->getColspan() >= 2;

$horizontal = self::DISPLAY_ORIENTATION_HORIZONTAL === $this->displayOrientation;
$vertical = self::DISPLAY_ORIENTATION_VERTICAL === $this->displayOrientation;

$rows = [];
if ($horizontal) {
foreach ($this->headers[0] ?? [] as $i => $header) {
$rows[$i] = [$header];
foreach ($this->rows as $row) {
Expand All @@ -308,13 +326,42 @@ public function render()
}
if (isset($row[$i])) {
$rows[$i][] = $row[$i];
} elseif ($rows[$i][0] instanceof TableCell && $rows[$i][0]->getColspan() >= 2) {
} elseif ($isCellWithColspan($rows[$i][0])) {
// Noop, there is a "title"
} else {
$rows[$i][] = null;
}
}
}
} elseif ($vertical) {
$maxHeaderLength = array_reduce($this->headers[0] ?? [], static fn (int $max, string $header) => max($max, mb_strlen($header)), 0);

foreach ($this->rows as $row) {
if ($row instanceof TableSeparator) {
continue;
}

if (0 < \count($rows)) {
$rows[] = [$divider];
}

$containsColspan = 0 < \count(array_filter($row, $isCellWithColspan));

$headers = $this->headers[0] ?? [];
$maxRows = max(\count($headers), \count($row));
for ($i = 0; $i < $maxRows; ++$i) {
$cell = (string) ($row[$i] ?? '');
if ([] !== $headers && !$containsColspan) {
$rows[] = [sprintf(
'<comment>%s</>: %s',
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
$cell
)];
} elseif (!empty($cell)) {
$rows[] = [$cell];
}
}
}
} else {
$rows = array_merge($this->headers, [$divider], $this->rows);
}
Expand All @@ -324,8 +371,8 @@ public function render()
$rowGroups = $this->buildTableRows($rows);
$this->calculateColumnsWidth($rowGroups);

$isHeader = !$this->horizontal;
$isFirstRow = $this->horizontal;
$isHeader = !$horizontal;
$isFirstRow = $horizontal;
$hasTitle = (bool) $this->headerTitle;

foreach ($rowGroups as $rowGroup) {
Expand Down Expand Up @@ -369,7 +416,12 @@ public function render()
$hasTitle = false;
}

if ($this->horizontal) {
if ($vertical) {
$isHeader = false;
$isFirstRow = false;
}

if ($horizontal) {
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
} else {
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
Expand Down
Loading
0