From ca11772e3faea1f5b43ceae1fc6df6714d9a08b7 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Mon, 3 Aug 2020 16:33:46 +0200 Subject: [PATCH] [Console] Table: support cells with newlines after a cell with colspan >= 2 --- .../Component/Console/Helper/Table.php | 16 ++++++-- .../Console/Tests/Helper/TableTest.php | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 756b8465ba..0e31e60d5c 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -574,6 +574,9 @@ class Table if (0 === $lineKey) { $rows[$rowKey][$column] = $line; } else { + if (!\array_key_exists($rowKey, $unmergedRows) || !\array_key_exists($lineKey, $unmergedRows[$rowKey])) { + $unmergedRows[$rowKey][$lineKey] = $this->copyRow($rows, $rowKey); + } $unmergedRows[$rowKey][$lineKey][$column] = $line; } } @@ -585,8 +588,8 @@ class Table yield $this->fillCells($row); if (isset($unmergedRows[$rowKey])) { - foreach ($unmergedRows[$rowKey] as $row) { - yield $row; + foreach ($unmergedRows[$rowKey] as $unmergedRow) { + yield $this->fillCells($unmergedRow); } } } @@ -670,12 +673,17 @@ class Table private function fillCells($row) { $newRow = []; + + $newColumn = null; foreach ($row as $column => $cell) { - $newRow[] = $cell; + if (null === $newColumn) { + $newColumn = $column; + } + $newRow[$newColumn++] = $cell; if ($cell instanceof TableCell && $cell->getColspan() > 1) { foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) { // insert empty value at column position - $newRow[] = ''; + $newRow[$newColumn++] = ''; } } } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index daa09feed0..070b87b035 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -333,6 +333,45 @@ TABLE | Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! | +-------------------------------+-------------------------------+-----------------------------+ +TABLE + ], + 'Cell after colspan contains new line break' => [ + ['Foo', 'Bar', 'Baz'], + [ + [ + new TableCell("foo\nbar", ['colspan' => 2]), + "baz\nqux", + ], + ], + 'default', +<<<'TABLE' ++-----+-----+-----+ +| Foo | Bar | Baz | ++-----+-----+-----+ +| foo | baz | +| bar | qux | ++-----+-----+-----+ + +TABLE + ], + 'Cell after colspan contains multiple new lines' => [ + ['Foo', 'Bar', 'Baz'], + [ + [ + new TableCell("foo\nbar", ['colspan' => 2]), + "baz\nqux\nquux", + ], + ], + 'default', +<<<'TABLE' ++-----+-----+------+ +| Foo | Bar | Baz | ++-----+-----+------+ +| foo | baz | +| bar | qux | +| | quux | ++-----+-----+------+ + TABLE ], 'Cell with rowspan' => [