Skip to content

[Console] Adding optional columns to Table #47448

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

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Ability to set optional|drop-able columns
  • Loading branch information
jlslew committed Sep 7, 2022
commit 51a686f7dc7096ff4da6d3612c44fbcc2a244880
55 changes: 55 additions & 0 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Table
private array $columnStyles = [];
private array $columnWidths = [];
private array $columnMaxWidths = [];
private array $optionalColumns = [];
private array $droppedColumns = [];
private int $maxWidth = 0;
private bool $rendered = false;
private string $displayOrientation = self::DISPLAY_ORIENTATION_DEFAULT;

Expand Down Expand Up @@ -174,6 +177,18 @@ public function setColumnMaxWidth(int $columnIndex, int $width): static
return $this;
}

public function setOptionalColumns(array $columns): static
{
$this->optionalColumns = $columns;
return $this;
}

public function setMaxWidth(int $maxWidth): static
{
$this->maxWidth = $maxWidth;
return $this;
}

/**
* @return $this
*/
Expand Down Expand Up @@ -402,6 +417,16 @@ public function render()
continue;
}

if ($this->droppedColumns) {
foreach ($this->droppedColumns as $column) {
if ($this->numberOfColumns < count($row)) {
unset($row[$column]);
}
}

$row = array_values($row);
}

if ($isHeader && !$isHeaderSeparatorRendered) {
$this->renderRowSeparator(
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
Expand Down Expand Up @@ -815,6 +840,36 @@ private function calculateColumnsWidth(iterable $groups)

$this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($this->style->getCellRowContentFormat()) - 2;
}

$effectiveColumnWidths = $this->effectiveColumnWidths;

for ($column = $this->numberOfColumns; $this->numberOfColumns > 0; --$column) {
if ($this->maxWidth && $this->maxWidth > array_sum($effectiveColumnWidths)) {
break;
}

$droppedColumn = array_pop($this->optionalColumns);
unset($effectiveColumnWidths[$droppedColumn]);
$this->droppedColumns[] = $droppedColumn;
}

if ($this->droppedColumns) {
foreach ($this->droppedColumns as $column) {
unset(
$this->effectiveColumnWidths[$column],
$this->columnMaxWidths[$column],
$this->columnStyles[$column],
$this->columnWidths[$column],
);
}

$this->effectiveColumnWidths = array_values($this->effectiveColumnWidths);
$this->columnMaxWidths = array_values($this->columnMaxWidths);
$this->columnStyles = array_values($this->columnStyles);
$this->columnWidths = array_values($this->columnWidths);

$this->numberOfColumns -= count($this->droppedColumns);
}
}

private function getColumnSeparatorWidth(): int
Expand Down