Skip to content

Fix table header seperator wrapping #45565

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

Merged
merged 1 commit into from
Apr 9, 2022
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
93 changes: 57 additions & 36 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,41 +369,59 @@ public function render()

$this->calculateNumberOfColumns($rows);

$rows = $this->buildTableRows($rows);
$this->calculateColumnsWidth($rows);
$rowGroups = $this->buildTableRows($rows);
$this->calculateColumnsWidth($rowGroups);

$isHeader = !$this->horizontal;
$isFirstRow = $this->horizontal;
$hasTitle = (bool) $this->headerTitle;
foreach ($rows as $row) {
if ($divider === $row) {
$isHeader = false;
$isFirstRow = true;

continue;
}
if ($row instanceof TableSeparator) {
$this->renderRowSeparator();
foreach ($rowGroups as $rowGroup) {
$isHeaderSeparatorRendered = false;

continue;
}
if (!$row) {
continue;
}
foreach ($rowGroup as $row) {
if ($divider === $row) {
$isHeader = false;
$isFirstRow = true;

if ($isHeader || $isFirstRow) {
$this->renderRowSeparator(
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
$hasTitle ? $this->headerTitle : null,
$hasTitle ? $this->style->getHeaderTitleFormat() : null
);
$isFirstRow = false;
$hasTitle = false;
}
if ($this->horizontal) {
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
} else {
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
continue;
}

if ($row instanceof TableSeparator) {
$this->renderRowSeparator();

continue;
}

if (!$row) {
continue;
}

if ($isHeader && !$isHeaderSeparatorRendered) {
$this->renderRowSeparator(
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
$hasTitle ? $this->headerTitle : null,
$hasTitle ? $this->style->getHeaderTitleFormat() : null
);
$hasTitle = false;
$isHeaderSeparatorRendered = true;
}

if ($isFirstRow) {
$this->renderRowSeparator(
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
$hasTitle ? $this->headerTitle : null,
$hasTitle ? $this->style->getHeaderTitleFormat() : null
);
$isFirstRow = false;
$hasTitle = false;
}

if ($this->horizontal) {
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
} else {
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
}
}
}
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
Expand Down Expand Up @@ -587,13 +605,14 @@ private function buildTableRows(array $rows): TableRows

return new TableRows(function () use ($rows, $unmergedRows): \Traversable {
foreach ($rows as $rowKey => $row) {
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
$rowGroup = [$row instanceof TableSeparator ? $row : $this->fillCells($row)];

if (isset($unmergedRows[$rowKey])) {
foreach ($unmergedRows[$rowKey] as $row) {
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
$rowGroup[] = $row instanceof TableSeparator ? $row : $this->fillCells($row);
}
}
yield $rowGroup;
}
});
}
Expand Down Expand Up @@ -734,14 +753,15 @@ private function getRowColumns(array $row): array
/**
* Calculates columns widths.
*/
private function calculateColumnsWidth(iterable $rows)
private function calculateColumnsWidth(iterable $groups)
{
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
$lengths = [];
foreach ($rows as $row) {
if ($row instanceof TableSeparator) {
continue;
}
foreach ($groups as $group) {
foreach ($group as $row) {
if ($row instanceof TableSeparator) {
continue;
}

foreach ($row as $i => $cell) {
if ($cell instanceof TableCell) {
Expand All @@ -756,7 +776,8 @@ private function calculateColumnsWidth(iterable $rows)
}
}

$lengths[] = $this->getCellWidth($row, $column);
$lengths[] = $this->getCellWidth($row, $column);
}
}

$this->effectiveColumnWidths[$column] = max($lengths) + Helper::strlen($this->style->getCellRowContentFormat()) - 2;
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,41 @@ public function testColumnMaxWidths()
| | ities | | |
+---------------+-------+------------+-----------------+

TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
}

public function testColumnMaxWidthsHeaders()
{
$table = new Table($output = $this->getOutputStream());
$table
->setHeaders([
[
'Publication',
'Very long header with a lot of information',
],
])
->setRows([
[
'1954',
'The Lord of the Rings, by J.R.R. Tolkien',
],
])
->setColumnMaxWidth(1, 30);

$table->render();

$expected =
<<<TABLE
+-------------+--------------------------------+
| Publication | Very long header with a lot of |
| | information |
+-------------+--------------------------------+
| 1954 | The Lord of the Rings, by J.R. |
| | R. Tolkien |
+-------------+--------------------------------+

TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
Expand Down