Skip to content
Merged
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
44 changes: 42 additions & 2 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,48 @@ private function buildTableRows(array $rows): TableRows
foreach ($rows[$rowKey] as $column => $cell) {
$colspan = $cell instanceof TableCell ? $cell->getColspan() : 1;

if (isset($this->columnMaxWidths[$column]) && Helper::width(Helper::removeDecoration($formatter, $cell)) > $this->columnMaxWidths[$column]) {
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan);
$minWrappedWidth = 0;
$widthApplied = [];
$lengthColumnBorder = $this->getColumnSeparatorWidth() + Helper::width($this->style->getCellRowContentFormat()) - 2;
for ($i = $column; $i < ($column + $colspan); ++$i) {
if (isset($this->columnMaxWidths[$i])) {
$minWrappedWidth += $this->columnMaxWidths[$i];
$widthApplied[] = ['type' => 'max', 'column' => $i];
} elseif (($this->columnWidths[$i] ?? 0) > 0 && $colspan > 1) {
$minWrappedWidth += $this->columnWidths[$i];
$widthApplied[] = ['type' => 'min', 'column' => $i];
}
}
if (1 === \count($widthApplied)) {
if ($colspan > 1) {
$minWrappedWidth *= $colspan; // previous logic
}
} elseif (\count($widthApplied) > 1) {
$minWrappedWidth += (\count($widthApplied) - 1) * $lengthColumnBorder;
}

$cellWidth = Helper::width(Helper::removeDecoration($formatter, $cell));
if ($minWrappedWidth && $cellWidth > $minWrappedWidth) {
$cell = $formatter->formatAndWrap($cell, $minWrappedWidth);
}
// update minimal columnWidths for spanned columns
if ($colspan > 1 && $minWrappedWidth > 0) {
$columnsMinWidthProcessed = [];
$cellWidth = min($cellWidth, $minWrappedWidth);
foreach ($widthApplied as $item) {
if ('max' === $item['type'] && $cellWidth >= $this->columnMaxWidths[$item['column']]) {
$minWidthColumn = $this->columnMaxWidths[$item['column']];
$this->columnWidths[$item['column']] = $minWidthColumn;
$columnsMinWidthProcessed[$item['column']] = true;
$cellWidth -= $minWidthColumn + $lengthColumnBorder;
}
}
for ($i = $column; $i < ($column + $colspan); ++$i) {
if (isset($columnsMinWidthProcessed[$i])) {
continue;
}
$this->columnWidths[$i] = $cellWidth + $lengthColumnBorder;
}
}
if (!str_contains($cell ?? '', "\n")) {
continue;
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1576,17 +1576,17 @@ public function testWithColspanAndMaxWith()
$expected =
<<<TABLE
+-----------------+-----------------+-----------------+
| Lorem ipsum dolor sit amet, consectetur adipi |
| scing elit, sed do eiusmod tempor |
| Lorem ipsum dolor sit amet, consectetur adipiscing |
| elit, sed do eiusmod tempor |
+-----------------+-----------------+-----------------+
| Lorem ipsum dolor sit amet, consectetur |
| adipiscing elit, sed do eiusmod tempor |
| Lorem ipsum dolor sit amet, consectetur adipiscing |
| elit, sed do eiusmod tempor |
+-----------------+-----------------+-----------------+
| Lorem ipsum dolor sit amet, co | hello world |
| nsectetur | |
| Lorem ipsum dolor sit amet, conse | hello world |
| ctetur | |
+-----------------+-----------------+-----------------+
| hello world | Lorem ipsum dolor sit amet, co |
| | nsectetur adipiscing elit |
| hello world | Lorem ipsum dolor sit amet, conse |
| | ctetur adipiscing elit |
+-----------------+-----------------+-----------------+
| hello | world | Lorem ipsum |
| | | dolor sit amet, |
Expand Down
Loading