diff --git a/Formatter/NullOutputFormatterStyle.php b/Formatter/NullOutputFormatterStyle.php index bfd0afedd..9232510f4 100644 --- a/Formatter/NullOutputFormatterStyle.php +++ b/Formatter/NullOutputFormatterStyle.php @@ -1,4 +1,5 @@ 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()); @@ -562,13 +580,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; } }); } @@ -709,14 +728,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) { @@ -731,7 +751,8 @@ private function calculateColumnsWidth(iterable $rows) } } - $lengths[] = $this->getCellWidth($row, $column); + $lengths[] = $this->getCellWidth($row, $column); + } } $this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($this->style->getCellRowContentFormat()) - 2; diff --git a/Tests/Command/DumpCompletionCommandTest.php b/Tests/Command/DumpCompletionCommandTest.php index de8a3d4a6..21df42873 100644 --- a/Tests/Command/DumpCompletionCommandTest.php +++ b/Tests/Command/DumpCompletionCommandTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Tests\Command; use PHPUnit\Framework\TestCase; diff --git a/Tests/Helper/ProgressIndicatorTest.php b/Tests/Helper/ProgressIndicatorTest.php index bbbde217c..cd97d406e 100644 --- a/Tests/Helper/ProgressIndicatorTest.php +++ b/Tests/Helper/ProgressIndicatorTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Tests\Helper; use PHPUnit\Framework\TestCase; diff --git a/Tests/Helper/QuestionHelperTest.php b/Tests/Helper/QuestionHelperTest.php index 1297b92f9..95ab1b302 100644 --- a/Tests/Helper/QuestionHelperTest.php +++ b/Tests/Helper/QuestionHelperTest.php @@ -714,9 +714,6 @@ public function testNoInteraction() $this->assertEquals('not yet', $dialog->ask($this->createStreamableInputInterfaceMock(null, false), $this->createOutputInterface(), $question)); } - /** - * @requires function mb_strwidth - */ public function testChoiceOutputFormattingQuestionForUtf8Keys() { $question = 'Lorem ipsum?'; diff --git a/Tests/Helper/SymfonyQuestionHelperTest.php b/Tests/Helper/SymfonyQuestionHelperTest.php index ed0800852..7877aabaa 100644 --- a/Tests/Helper/SymfonyQuestionHelperTest.php +++ b/Tests/Helper/SymfonyQuestionHelperTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfony\Component\Console\Tests\Helper; use Symfony\Component\Console\Exception\RuntimeException; diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index eeca87e81..8465c51dc 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -1365,6 +1365,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 = + <<