Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69f7f6e

Browse files
committed
bug #45565 Fix table header seperator wrapping (alamirault)
This PR was merged into the 4.4 branch. Discussion ---------- Fix table header seperator wrapping | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45520 | License | MIT | Doc PR | Before PR, a new seperator is added foreach split line in header ```php $table = new Table(); $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); ``` Before PR: ![image](https://user-images.githubusercontent.com/9253091/155845175-94482e50-8507-4762-9da1-dbc88967bd47.png) After PR ![image](https://user-images.githubusercontent.com/9253091/155845154-3182ec3a-eaa8-423f-a1bc-8744c2a95a88.png) (Fabbot cannot be green (break unit tests)) Commits ------- ebf38b1 [Console] Header with column max width is now well wrap with separator
2 parents 7f0edd6 + ebf38b1 commit 69f7f6e

File tree

2 files changed

+92
-36
lines changed

2 files changed

+92
-36
lines changed

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

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -369,41 +369,59 @@ public function render()
369369

370370
$this->calculateNumberOfColumns($rows);
371371

372-
$rows = $this->buildTableRows($rows);
373-
$this->calculateColumnsWidth($rows);
372+
$rowGroups = $this->buildTableRows($rows);
373+
$this->calculateColumnsWidth($rowGroups);
374374

375375
$isHeader = !$this->horizontal;
376376
$isFirstRow = $this->horizontal;
377377
$hasTitle = (bool) $this->headerTitle;
378-
foreach ($rows as $row) {
379-
if ($divider === $row) {
380-
$isHeader = false;
381-
$isFirstRow = true;
382378

383-
continue;
384-
}
385-
if ($row instanceof TableSeparator) {
386-
$this->renderRowSeparator();
379+
foreach ($rowGroups as $rowGroup) {
380+
$isHeaderSeparatorRendered = false;
387381

388-
continue;
389-
}
390-
if (!$row) {
391-
continue;
392-
}
382+
foreach ($rowGroup as $row) {
383+
if ($divider === $row) {
384+
$isHeader = false;
385+
$isFirstRow = true;
393386

394-
if ($isHeader || $isFirstRow) {
395-
$this->renderRowSeparator(
396-
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
397-
$hasTitle ? $this->headerTitle : null,
398-
$hasTitle ? $this->style->getHeaderTitleFormat() : null
399-
);
400-
$isFirstRow = false;
401-
$hasTitle = false;
402-
}
403-
if ($this->horizontal) {
404-
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
405-
} else {
406-
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
387+
continue;
388+
}
389+
390+
if ($row instanceof TableSeparator) {
391+
$this->renderRowSeparator();
392+
393+
continue;
394+
}
395+
396+
if (!$row) {
397+
continue;
398+
}
399+
400+
if ($isHeader && !$isHeaderSeparatorRendered) {
401+
$this->renderRowSeparator(
402+
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
403+
$hasTitle ? $this->headerTitle : null,
404+
$hasTitle ? $this->style->getHeaderTitleFormat() : null
405+
);
406+
$hasTitle = false;
407+
$isHeaderSeparatorRendered = true;
408+
}
409+
410+
if ($isFirstRow) {
411+
$this->renderRowSeparator(
412+
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
413+
$hasTitle ? $this->headerTitle : null,
414+
$hasTitle ? $this->style->getHeaderTitleFormat() : null
415+
);
416+
$isFirstRow = false;
417+
$hasTitle = false;
418+
}
419+
420+
if ($this->horizontal) {
421+
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
422+
} else {
423+
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
424+
}
407425
}
408426
}
409427
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
@@ -587,13 +605,14 @@ private function buildTableRows(array $rows): TableRows
587605

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

592610
if (isset($unmergedRows[$rowKey])) {
593611
foreach ($unmergedRows[$rowKey] as $row) {
594-
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
612+
$rowGroup[] = $row instanceof TableSeparator ? $row : $this->fillCells($row);
595613
}
596614
}
615+
yield $rowGroup;
597616
}
598617
});
599618
}
@@ -734,14 +753,15 @@ private function getRowColumns(array $row): array
734753
/**
735754
* Calculates columns widths.
736755
*/
737-
private function calculateColumnsWidth(iterable $rows)
756+
private function calculateColumnsWidth(iterable $groups)
738757
{
739758
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
740759
$lengths = [];
741-
foreach ($rows as $row) {
742-
if ($row instanceof TableSeparator) {
743-
continue;
744-
}
760+
foreach ($groups as $group) {
761+
foreach ($group as $row) {
762+
if ($row instanceof TableSeparator) {
763+
continue;
764+
}
745765

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

759-
$lengths[] = $this->getCellWidth($row, $column);
779+
$lengths[] = $this->getCellWidth($row, $column);
780+
}
760781
}
761782

762783
$this->effectiveColumnWidths[$column] = max($lengths) + Helper::strlen($this->style->getCellRowContentFormat()) - 2;

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,41 @@ public function testColumnMaxWidths()
11581158
| | ities | | |
11591159
+---------------+-------+------------+-----------------+
11601160
1161+
TABLE;
1162+
1163+
$this->assertEquals($expected, $this->getOutputContent($output));
1164+
}
1165+
1166+
public function testColumnMaxWidthsHeaders()
1167+
{
1168+
$table = new Table($output = $this->getOutputStream());
1169+
$table
1170+
->setHeaders([
1171+
[
1172+
'Publication',
1173+
'Very long header with a lot of information',
1174+
],
1175+
])
1176+
->setRows([
1177+
[
1178+
'1954',
1179+
'The Lord of the Rings, by J.R.R. Tolkien',
1180+
],
1181+
])
1182+
->setColumnMaxWidth(1, 30);
1183+
1184+
$table->render();
1185+
1186+
$expected =
1187+
<<<TABLE
1188+
+-------------+--------------------------------+
1189+
| Publication | Very long header with a lot of |
1190+
| | information |
1191+
+-------------+--------------------------------+
1192+
| 1954 | The Lord of the Rings, by J.R. |
1193+
| | R. Tolkien |
1194+
+-------------+--------------------------------+
1195+
11611196
TABLE;
11621197

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

0 commit comments

Comments
 (0)