Skip to content

Commit 51a686f

Browse files
committed
Ability to set optional|drop-able columns
1 parent ef92fb5 commit 51a686f

File tree

1 file changed

+55
-0
lines changed
  • src/Symfony/Component/Console/Helper

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class Table
5050
private array $columnStyles = [];
5151
private array $columnWidths = [];
5252
private array $columnMaxWidths = [];
53+
private array $optionalColumns = [];
54+
private array $droppedColumns = [];
55+
private int $maxWidth = 0;
5356
private bool $rendered = false;
5457
private string $displayOrientation = self::DISPLAY_ORIENTATION_DEFAULT;
5558

@@ -174,6 +177,18 @@ public function setColumnMaxWidth(int $columnIndex, int $width): static
174177
return $this;
175178
}
176179

180+
public function setOptionalColumns(array $columns): static
181+
{
182+
$this->optionalColumns = $columns;
183+
return $this;
184+
}
185+
186+
public function setMaxWidth(int $maxWidth): static
187+
{
188+
$this->maxWidth = $maxWidth;
189+
return $this;
190+
}
191+
177192
/**
178193
* @return $this
179194
*/
@@ -402,6 +417,16 @@ public function render()
402417
continue;
403418
}
404419

420+
if ($this->droppedColumns) {
421+
foreach ($this->droppedColumns as $column) {
422+
if ($this->numberOfColumns < count($row)) {
423+
unset($row[$column]);
424+
}
425+
}
426+
427+
$row = array_values($row);
428+
}
429+
405430
if ($isHeader && !$isHeaderSeparatorRendered) {
406431
$this->renderRowSeparator(
407432
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
@@ -815,6 +840,36 @@ private function calculateColumnsWidth(iterable $groups)
815840

816841
$this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($this->style->getCellRowContentFormat()) - 2;
817842
}
843+
844+
$effectiveColumnWidths = $this->effectiveColumnWidths;
845+
846+
for ($column = $this->numberOfColumns; $this->numberOfColumns > 0; --$column) {
847+
if ($this->maxWidth && $this->maxWidth > array_sum($effectiveColumnWidths)) {
848+
break;
849+
}
850+
851+
$droppedColumn = array_pop($this->optionalColumns);
852+
unset($effectiveColumnWidths[$droppedColumn]);
853+
$this->droppedColumns[] = $droppedColumn;
854+
}
855+
856+
if ($this->droppedColumns) {
857+
foreach ($this->droppedColumns as $column) {
858+
unset(
859+
$this->effectiveColumnWidths[$column],
860+
$this->columnMaxWidths[$column],
861+
$this->columnStyles[$column],
862+
$this->columnWidths[$column],
863+
);
864+
}
865+
866+
$this->effectiveColumnWidths = array_values($this->effectiveColumnWidths);
867+
$this->columnMaxWidths = array_values($this->columnMaxWidths);
868+
$this->columnStyles = array_values($this->columnStyles);
869+
$this->columnWidths = array_values($this->columnWidths);
870+
871+
$this->numberOfColumns -= count($this->droppedColumns);
872+
}
818873
}
819874

820875
private function getColumnSeparatorWidth(): int

0 commit comments

Comments
 (0)