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

Skip to content

Commit 9ee4440

Browse files
committed
Header with column max width is now well wrap with separator
1 parent 9bc4468 commit 9ee4440

File tree

2 files changed

+104
-46
lines changed

2 files changed

+104
-46
lines changed

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

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -383,41 +383,59 @@ public function render()
383383

384384
$this->calculateNumberOfColumns($rows);
385385

386-
$rows = $this->buildTableRows($rows);
387-
$this->calculateColumnsWidth($rows);
386+
$rowGroups = $this->buildTableRows($rows);
387+
$this->calculateColumnsWidth($rowGroups);
388388

389389
$isHeader = !$this->horizontal;
390390
$isFirstRow = $this->horizontal;
391391
$hasTitle = (bool) $this->headerTitle;
392-
foreach ($rows as $row) {
393-
if ($divider === $row) {
394-
$isHeader = false;
395-
$isFirstRow = true;
396392

397-
continue;
398-
}
399-
if ($row instanceof TableSeparator) {
400-
$this->renderRowSeparator();
393+
foreach ($rowGroups as $rowGroup) {
394+
$isHeaderSeparatorRendered = false;
401395

402-
continue;
403-
}
404-
if (!$row) {
405-
continue;
406-
}
396+
foreach ($rowGroup as $row) {
397+
if ($divider === $row) {
398+
$isHeader = false;
399+
$isFirstRow = true;
407400

408-
if ($isHeader || $isFirstRow) {
409-
$this->renderRowSeparator(
410-
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
411-
$hasTitle ? $this->headerTitle : null,
412-
$hasTitle ? $this->style->getHeaderTitleFormat() : null
413-
);
414-
$isFirstRow = false;
415-
$hasTitle = false;
416-
}
417-
if ($this->horizontal) {
418-
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
419-
} else {
420-
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
401+
continue;
402+
}
403+
404+
if ($row instanceof TableSeparator) {
405+
$this->renderRowSeparator();
406+
407+
continue;
408+
}
409+
410+
if (!$row) {
411+
continue;
412+
}
413+
414+
if ($isHeader && !$isHeaderSeparatorRendered) {
415+
$this->renderRowSeparator(
416+
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
417+
$hasTitle ? $this->headerTitle : null,
418+
$hasTitle ? $this->style->getHeaderTitleFormat() : null
419+
);
420+
$hasTitle = false;
421+
$isHeaderSeparatorRendered = true;
422+
}
423+
424+
if ($isFirstRow) {
425+
$this->renderRowSeparator(
426+
$isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM,
427+
$hasTitle ? $this->headerTitle : null,
428+
$hasTitle ? $this->style->getHeaderTitleFormat() : null
429+
);
430+
$isFirstRow = false;
431+
$hasTitle = false;
432+
}
433+
434+
if ($this->horizontal) {
435+
$this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat());
436+
} else {
437+
$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
438+
}
421439
}
422440
}
423441
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
@@ -624,13 +642,16 @@ private function buildTableRows(array $rows): TableRows
624642

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

629649
if (isset($unmergedRows[$rowKey])) {
630650
foreach ($unmergedRows[$rowKey] as $row) {
631-
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
651+
$rowGroup[] = $row instanceof TableSeparator ? $row : $this->fillCells($row);
632652
}
633653
}
654+
yield $rowGroup;
634655
}
635656
});
636657
}
@@ -659,7 +680,7 @@ private function fillNextRows(array $rows, int $line): array
659680
{
660681
$unmergedRows = [];
661682
foreach ($rows[$line] as $column => $cell) {
662-
if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
683+
if (null !== $cell && !$cell instanceof TableCell && !\is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
663684
throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given.', get_debug_type($cell)));
664685
}
665686
if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
@@ -771,29 +792,31 @@ private function getRowColumns(array $row): array
771792
/**
772793
* Calculates columns widths.
773794
*/
774-
private function calculateColumnsWidth(iterable $rows)
795+
private function calculateColumnsWidth(iterable $groups)
775796
{
776797
for ($column = 0; $column < $this->numberOfColumns; ++$column) {
777798
$lengths = [];
778-
foreach ($rows as $row) {
779-
if ($row instanceof TableSeparator) {
780-
continue;
781-
}
799+
foreach ($groups as $group) {
800+
foreach ($group as $row) {
801+
if ($row instanceof TableSeparator) {
802+
continue;
803+
}
782804

783-
foreach ($row as $i => $cell) {
784-
if ($cell instanceof TableCell) {
785-
$textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
786-
$textLength = Helper::width($textContent);
787-
if ($textLength > 0) {
788-
$contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
789-
foreach ($contentColumns as $position => $content) {
790-
$row[$i + $position] = $content;
805+
foreach ($row as $i => $cell) {
806+
if ($cell instanceof TableCell) {
807+
$textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
808+
$textLength = Helper::width($textContent);
809+
if ($textLength > 0) {
810+
$contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
811+
foreach ($contentColumns as $position => $content) {
812+
$row[$i + $position] = $content;
813+
}
791814
}
792815
}
793816
}
794-
}
795817

796-
$lengths[] = $this->getCellWidth($row, $column);
818+
$lengths[] = $this->getCellWidth($row, $column);
819+
}
797820
}
798821

799822
$this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($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
@@ -1365,6 +1365,41 @@ public function testColumnMaxWidths()
13651365
| | ities | | |
13661366
+---------------+-------+------------+-----------------+
13671367
1368+
TABLE;
1369+
1370+
$this->assertEquals($expected, $this->getOutputContent($output));
1371+
}
1372+
1373+
public function testColumnMaxWidthsHeaders()
1374+
{
1375+
$table = new Table($output = $this->getOutputStream());
1376+
$table
1377+
->setHeaders([
1378+
[
1379+
'Publication',
1380+
'Very long header with a lot of information',
1381+
],
1382+
])
1383+
->setRows([
1384+
[
1385+
'1954',
1386+
'The Lord of the Rings, by J.R.R. Tolkien',
1387+
],
1388+
])
1389+
->setColumnMaxWidth(1, 30);
1390+
1391+
$table->render();
1392+
1393+
$expected =
1394+
<<<TABLE
1395+
+-------------+--------------------------------+
1396+
| Publication | Very long header with a lot of |
1397+
| | information |
1398+
+-------------+--------------------------------+
1399+
| 1954 | The Lord of the Rings, by J.R. |
1400+
| | R. Tolkien |
1401+
+-------------+--------------------------------+
1402+
13681403
TABLE;
13691404

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

0 commit comments

Comments
 (0)