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

Skip to content

Commit 1cf9659

Browse files
committed
fix buildTableRows when Colspan is use with content too long
1 parent 4a6a925 commit 1cf9659

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ private function applyCurrentStyle(string $text, string $current, int $width, in
263263
}
264264

265265
$lines = explode("\n", $text);
266-
if ($width === $currentLineLength = \strlen(end($lines))) {
267-
$currentLineLength = 0;
266+
267+
foreach ($lines as $line) {
268+
$currentLineLength += \strlen($line);
269+
if ($width <= $currentLineLength) {
270+
$currentLineLength = 0;
271+
}
268272
}
269273

270274
if ($this->isDecorated()) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,18 @@ private function buildTableRows($rows)
522522

523523
// Remove any new line breaks and replace it with a new line
524524
foreach ($rows[$rowKey] as $column => $cell) {
525+
$colspan = $cell instanceof TableCell ? $cell->getColspan() : 1;
526+
525527
if (isset($this->columnMaxWidths[$column]) && Helper::strlenWithoutDecoration($formatter, $cell) > $this->columnMaxWidths[$column]) {
526-
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column]);
528+
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan);
527529
}
528530
if (!strstr($cell, "\n")) {
529531
continue;
530532
}
531533
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
532534
foreach ($lines as $lineKey => $line) {
533-
if ($cell instanceof TableCell) {
534-
$line = new TableCell($line, ['colspan' => $cell->getColspan()]);
535+
if ($colspan > 1) {
536+
$line = new TableCell($line, ['colspan' => $colspan]);
535537
}
536538
if (0 === $lineKey) {
537539
$rows[$rowKey][$column] = $line;

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ public function testFormatAndWrap()
332332
$this->assertSame("pre\e[37;41m\e[39;49m\n\e[37;41mfoo\e[39;49m\n\e[37;41mbar\e[39;49m\n\e[37;41mbaz\e[39;49m\npos\nt", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 3));
333333
$this->assertSame("pre \e[37;41m\e[39;49m\n\e[37;41mfoo \e[39;49m\n\e[37;41mbar \e[39;49m\n\e[37;41mbaz\e[39;49m \npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 4));
334334
$this->assertSame("pre \e[37;41mf\e[39;49m\n\e[37;41moo ba\e[39;49m\n\e[37;41mr baz\e[39;49m\npost", $formatter->formatAndWrap('pre <error>foo bar baz</error> post', 5));
335+
$this->assertSame("Lore\nm \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m \ndolo\nr \e[32msi\e[39m\n\e[32mt\e[39m am\net", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info> amet', 4));
336+
$this->assertSame("Lorem \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m dolo\nr \e[32msit\e[39m am\net", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info> amet', 8));
337+
$this->assertSame("Lorem \e[37;41mipsum\e[39;49m dolor \e[32m\e[39m\n\e[32msit\e[39m, \e[37;41mamet\e[39;49m et \e[32mlauda\e[39m\n\e[32mntium\e[39m architecto", $formatter->formatAndWrap('Lorem <error>ipsum</error> dolor <info>sit</info>, <error>amet</error> et <info>laudantium</info> architecto', 18));
335338

336339
$formatter = new OutputFormatter();
337340

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,4 +1124,56 @@ protected function getOutputContent(StreamOutput $output)
11241124

11251125
return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
11261126
}
1127+
1128+
public function testWithColspanAndMaxWith(): void
1129+
{
1130+
$table = new Table($output = $this->getOutputStream());
1131+
1132+
$table->setColumnMaxWidth(0, 15);
1133+
$table->setColumnMaxWidth(1, 15);
1134+
$table->setColumnMaxWidth(2, 15);
1135+
$table->setRows([
1136+
[new TableCell('Lorem ipsum dolor sit amet, <fg=white;bg=green>consectetur</> adipiscing elit, <fg=white;bg=red>sed</> do <fg=white;bg=red>eiusmod</> tempor', ['colspan' => 3])],
1137+
new TableSeparator(),
1138+
[new TableCell('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor', ['colspan' => 3])],
1139+
new TableSeparator(),
1140+
[new TableCell('Lorem ipsum <fg=white;bg=red>dolor</> sit amet, consectetur ', ['colspan' => 2]), 'hello world'],
1141+
new TableSeparator(),
1142+
['hello <fg=white;bg=green>world</>', new TableCell('Lorem ipsum dolor sit amet, <fg=white;bg=green>consectetur</> adipiscing elit', ['colspan' => 2])],
1143+
new TableSeparator(),
1144+
['hello ', new TableCell('world', ['colspan' => 1]), 'Lorem ipsum dolor sit amet, consectetur'],
1145+
new TableSeparator(),
1146+
['Symfony ', new TableCell('Test', ['colspan' => 1]), 'Lorem <fg=white;bg=green>ipsum</> dolor sit amet, consectetur'],
1147+
])
1148+
;
1149+
$table->render();
1150+
1151+
$expected =
1152+
<<<TABLE
1153+
+-----------------+-----------------+-----------------+
1154+
| Lorem ipsum dolor sit amet, consectetur adipi |
1155+
| scing elit, sed do eiusmod tempor |
1156+
+-----------------+-----------------+-----------------+
1157+
| Lorem ipsum dolor sit amet, consectetur adipi |
1158+
| scing elit, sed do eiusmod tempor |
1159+
+-----------------+-----------------+-----------------+
1160+
| Lorem ipsum dolor sit amet, co | hello world |
1161+
| nsectetur | |
1162+
+-----------------+-----------------+-----------------+
1163+
| hello world | Lorem ipsum dolor sit amet, co |
1164+
| | nsectetur adipiscing elit |
1165+
+-----------------+-----------------+-----------------+
1166+
| hello | world | Lorem ipsum dol |
1167+
| | | or sit amet, co |
1168+
| | | nsectetur |
1169+
+-----------------+-----------------+-----------------+
1170+
| Symfony | Test | Lorem ipsum dol |
1171+
| | | or sit amet, co |
1172+
| | | nsectetur |
1173+
+-----------------+-----------------+-----------------+
1174+
1175+
TABLE;
1176+
1177+
$this->assertSame($expected, $this->getOutputContent($output));
1178+
}
11271179
}

0 commit comments

Comments
 (0)