Skip to content

[Console] Table counts wrong column width when using colspan and setColumnMaxWidth() #60044

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 4 commits into
base: 6.4
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
issues/60043: Table counts wrong column width when use colspan and se…
…tColumnMaxWidth().
  • Loading branch information
vladimir-vv committed May 2, 2025
commit b725f05fdeba4e37e044ea672d0ade68bad93b68
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 @@ -632,8 +632,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];
} else if (($this->columnWidths[$i] ?? 0) > 0 && $colspan > 1) {
$minWrappedWidth += $this->columnWidths[$i];
$widthApplied[] = ['type' => 'min', 'column' => $i];
}
}
if (count($widthApplied) === 1) {
if ($colspan > 1) {
$minWrappedWidth = $minWrappedWidth * $colspan; // previous logic
}
} else if (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);
array_filter($widthApplied, function ($item) use (&$cellWidth, &$columnsMinWidthProcessed, $lengthColumnBorder) {
if ($item['type'] === 'max' && $cellWidth >= $this->columnMaxWidths[$item['column']]) {
$minWidthColumn = $this->columnMaxWidths[$item['column']];
$this->columnWidths[$item['column']] = $minWidthColumn;
$columnsMinWidthProcessed[] = $item['column'];
$cellWidth -= $minWidthColumn + $lengthColumnBorder;
}
});
for ($i = $column; $i < ($column + $colspan); $i++) {
if (in_array($i, $columnsMinWidthProcessed)) {
continue;
}
$this->columnWidths[$i] = $cellWidth + $lengthColumnBorder;
}
}
if (!str_contains($cell ?? '', "\n")) {
continue;
Expand Down