Skip to content

Commit 9056771

Browse files
vladimir-vvnicolas-grekas
authored andcommitted
[Console] Table counts wrong column width when using colspan and setColumnMaxWidth()
1 parent 2f21bcd commit 9056771

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

Helper/Table.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,48 @@ private function buildTableRows(array $rows): TableRows
629629
foreach ($rows[$rowKey] as $column => $cell) {
630630
$colspan = $cell instanceof TableCell ? $cell->getColspan() : 1;
631631

632-
if (isset($this->columnMaxWidths[$column]) && Helper::width(Helper::removeDecoration($formatter, $cell)) > $this->columnMaxWidths[$column]) {
633-
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan);
632+
$minWrappedWidth = 0;
633+
$widthApplied = [];
634+
$lengthColumnBorder = $this->getColumnSeparatorWidth() + Helper::width($this->style->getCellRowContentFormat()) - 2;
635+
for ($i = $column; $i < ($column + $colspan); ++$i) {
636+
if (isset($this->columnMaxWidths[$i])) {
637+
$minWrappedWidth += $this->columnMaxWidths[$i];
638+
$widthApplied[] = ['type' => 'max', 'column' => $i];
639+
} elseif (($this->columnWidths[$i] ?? 0) > 0 && $colspan > 1) {
640+
$minWrappedWidth += $this->columnWidths[$i];
641+
$widthApplied[] = ['type' => 'min', 'column' => $i];
642+
}
643+
}
644+
if (1 === \count($widthApplied)) {
645+
if ($colspan > 1) {
646+
$minWrappedWidth *= $colspan; // previous logic
647+
}
648+
} elseif (\count($widthApplied) > 1) {
649+
$minWrappedWidth += (\count($widthApplied) - 1) * $lengthColumnBorder;
650+
}
651+
652+
$cellWidth = Helper::width(Helper::removeDecoration($formatter, $cell));
653+
if ($minWrappedWidth && $cellWidth > $minWrappedWidth) {
654+
$cell = $formatter->formatAndWrap($cell, $minWrappedWidth);
655+
}
656+
// update minimal columnWidths for spanned columns
657+
if ($colspan > 1 && $minWrappedWidth > 0) {
658+
$columnsMinWidthProcessed = [];
659+
$cellWidth = min($cellWidth, $minWrappedWidth);
660+
foreach ($widthApplied as $item) {
661+
if ('max' === $item['type'] && $cellWidth >= $this->columnMaxWidths[$item['column']]) {
662+
$minWidthColumn = $this->columnMaxWidths[$item['column']];
663+
$this->columnWidths[$item['column']] = $minWidthColumn;
664+
$columnsMinWidthProcessed[$item['column']] = true;
665+
$cellWidth -= $minWidthColumn + $lengthColumnBorder;
666+
}
667+
}
668+
for ($i = $column; $i < ($column + $colspan); ++$i) {
669+
if (isset($columnsMinWidthProcessed[$i])) {
670+
continue;
671+
}
672+
$this->columnWidths[$i] = $cellWidth + $lengthColumnBorder;
673+
}
634674
}
635675
if (!str_contains($cell ?? '', "\n")) {
636676
continue;

Tests/Helper/TableTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,17 +1576,17 @@ public function testWithColspanAndMaxWith()
15761576
$expected =
15771577
<<<TABLE
15781578
+-----------------+-----------------+-----------------+
1579-
| Lorem ipsum dolor sit amet, consectetur adipi |
1580-
| scing elit, sed do eiusmod tempor |
1579+
| Lorem ipsum dolor sit amet, consectetur adipiscing |
1580+
| elit, sed do eiusmod tempor |
15811581
+-----------------+-----------------+-----------------+
1582-
| Lorem ipsum dolor sit amet, consectetur |
1583-
| adipiscing elit, sed do eiusmod tempor |
1582+
| Lorem ipsum dolor sit amet, consectetur adipiscing |
1583+
| elit, sed do eiusmod tempor |
15841584
+-----------------+-----------------+-----------------+
1585-
| Lorem ipsum dolor sit amet, co | hello world |
1586-
| nsectetur | |
1585+
| Lorem ipsum dolor sit amet, conse | hello world |
1586+
| ctetur | |
15871587
+-----------------+-----------------+-----------------+
1588-
| hello world | Lorem ipsum dolor sit amet, co |
1589-
| | nsectetur adipiscing elit |
1588+
| hello world | Lorem ipsum dolor sit amet, conse |
1589+
| | ctetur adipiscing elit |
15901590
+-----------------+-----------------+-----------------+
15911591
| hello | world | Lorem ipsum |
15921592
| | | dolor sit amet, |

0 commit comments

Comments
 (0)