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
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Improve truecolor terminal detection in some cases
* Add support for 256 color terminals (conversion from Ansi24 to Ansi8 if terminal is capable of it)
* Adding optional columns to Table

6.1
---
Expand Down
87 changes: 87 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,20 @@ 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 @@ -377,6 +394,11 @@ public function render()
$rowGroups = $this->buildTableRows($rows);
$this->calculateColumnsWidth($rowGroups);

if ($this->maxWidth && $this->optionalColumns) {
$this->dropColumn($rowGroups);
$this->calculateColumnsWidth($rowGroups);
}

$isHeader = !$horizontal;
$isFirstRow = $horizontal;
$hasTitle = (bool) $this->headerTitle;
Expand All @@ -402,6 +424,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 @@ -788,6 +820,7 @@ private function getRowColumns(array $row): array
*/
private function calculateColumnsWidth(iterable $groups)
{
$pass2 = \count($this->effectiveColumnWidths);
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
$lengths = [];
foreach ($groups as $group) {
Expand Down Expand Up @@ -815,6 +848,60 @@ private function calculateColumnsWidth(iterable $groups)

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

if ($pass2) {
$this->effectiveColumnWidths = array_values($this->effectiveColumnWidths);
$this->columnMaxWidths = array_values($this->columnMaxWidths);
$this->columnStyles = array_values($this->columnStyles);
$this->columnWidths = array_values($this->columnWidths);
}
}

private function dropColumn(iterable $groups)
{
$effectiveColumnWidths = $this->effectiveColumnWidths;
$optionalColumns = [];

foreach ($this->optionalColumns as $column) {
$optionalColumns[$column] = $effectiveColumnWidths[$column];
}

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

$largestOptionalColumn = array_keys($optionalColumns, max($optionalColumns))[0];
unset($effectiveColumnWidths[$largestOptionalColumn], $optionalColumns[$largestOptionalColumn]);
$this->droppedColumns[] = $largestOptionalColumn;

if (empty($optionalColumns)) {
break;
}
}

if ($this->droppedColumns) {
foreach ($this->droppedColumns as $column) {
foreach ($groups as $group) {
foreach ($group as $row) {
foreach ($row as $index => $cell) {
if ($cell instanceof TableCell && $index + $cell->getColspan() > $column) {
$cell->reduceColspan();
}
}
}
}

unset(
$this->effectiveColumnWidths[$column],
$this->columnMaxWidths[$column],
$this->columnStyles[$column],
$this->columnWidths[$column],
);
}

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

private function getColumnSeparatorWidth(): int
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Console/Helper/TableCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public function getColspan(): int
return (int) $this->options['colspan'];
}

/**
* Reduces number of colspan.
* Used by optional columns.
*/
public function reduceColspan(): void
{
if ((int) $this->options['colspan'] > 1) {
--$this->options['colspan'];
}
}

/**
* Gets number of rowspan.
*/
Expand Down