Skip to content

Commit 0290d9d

Browse files
committed
Handle multiple optional columns
1 parent 88b3448 commit 0290d9d

File tree

1 file changed

+21
-9
lines changed
  • src/Symfony/Component/Console/Helper

1 file changed

+21
-9
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ private function getRowColumns(array $row): array
816816
*/
817817
private function calculateColumnsWidth(iterable $groups)
818818
{
819+
$pass2 = count($this->effectiveColumnWidths);
819820
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
820821
$lengths = [];
821822
foreach ($groups as $group) {
@@ -843,28 +844,44 @@ private function calculateColumnsWidth(iterable $groups)
843844

844845
$this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($this->style->getCellRowContentFormat()) - 2;
845846
}
847+
848+
if ($pass2) {
849+
$this->effectiveColumnWidths = array_values($this->effectiveColumnWidths);
850+
$this->columnMaxWidths = array_values($this->columnMaxWidths);
851+
$this->columnStyles = array_values($this->columnStyles);
852+
$this->columnWidths = array_values($this->columnWidths);
853+
}
846854
}
847855

848856
private function dropColumn(iterable $groups)
849857
{
850858
$effectiveColumnWidths = $this->effectiveColumnWidths;
859+
$optionalColumns = [];
860+
861+
foreach ($this->optionalColumns as $column) {
862+
$optionalColumns[$column] = $effectiveColumnWidths[$column];
863+
}
851864

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

857-
$droppedColumn = array_pop($this->optionalColumns);
858-
unset($effectiveColumnWidths[$droppedColumn]);
859-
$this->droppedColumns[] = $droppedColumn;
870+
$largestOptionalColumn = array_keys($optionalColumns, max($optionalColumns))[0];
871+
unset($effectiveColumnWidths[$largestOptionalColumn], $optionalColumns[$largestOptionalColumn]);
872+
$this->droppedColumns[] = $largestOptionalColumn;
873+
874+
if (empty($optionalColumns)) {
875+
break;
876+
}
860877
}
861878

862879
if ($this->droppedColumns) {
863880
foreach ($this->droppedColumns as $column) {
864881
foreach ($groups as $group) {
865882
foreach ($group as $row) {
866883
foreach ($row as $index => $cell) {
867-
if ($cell instanceof TableCell && $index + $cell->getColspan() >= $column) {
884+
if ($cell instanceof TableCell && $index + $cell->getColspan() > $column) {
868885
$cell->reduceColspan();
869886
}
870887
}
@@ -879,11 +896,6 @@ private function dropColumn(iterable $groups)
879896
);
880897
}
881898

882-
$this->effectiveColumnWidths = array_values($this->effectiveColumnWidths);
883-
$this->columnMaxWidths = array_values($this->columnMaxWidths);
884-
$this->columnStyles = array_values($this->columnStyles);
885-
$this->columnWidths = array_values($this->columnWidths);
886-
887899
$this->numberOfColumns -= count($this->droppedColumns);
888900
}
889901
}

0 commit comments

Comments
 (0)