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

Skip to content

Commit c248646

Browse files
committed
bug #30852 [Console] fix buildTableRows when Colspan is use with content too long (Raulnet)
This PR was merged into the 4.2 branch. Discussion ---------- [Console] fix buildTableRows when Colspan is use with content too long | Q | A | ------------- | --- | Branch? | 4.2 for bug fixes | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes ( new test added TableTest::testWithColspanAndMaxWith) | Fixed tickets | #30701 | License | MIT | Doc PR | no <!-- fix for keeping ColumnMaxwith when Content is too long Commits ------- 1cf9659 fix buildTableRows when Colspan is use with content too long
2 parents f794e41 + 1cf9659 commit c248646

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
@@ -523,8 +523,10 @@ private function buildTableRows($rows)
523523

524524
// Remove any new line breaks and replace it with a new line
525525
foreach ($rows[$rowKey] as $column => $cell) {
526+
$colspan = $cell instanceof TableCell ? $cell->getColspan() : 1;
527+
526528
if (isset($this->columnMaxWidths[$column]) && Helper::strlenWithoutDecoration($formatter, $cell) > $this->columnMaxWidths[$column]) {
527-
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column]);
529+
$cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan);
528530
}
529531
if (!strstr($cell, "\n")) {
530532
continue;
@@ -533,8 +535,8 @@ private function buildTableRows($rows)
533535
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
534536
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
535537
foreach ($lines as $lineKey => $line) {
536-
if ($cell instanceof TableCell) {
537-
$line = new TableCell($line, ['colspan' => $cell->getColspan()]);
538+
if ($colspan > 1) {
539+
$line = new TableCell($line, ['colspan' => $colspan]);
538540
}
539541
if (0 === $lineKey) {
540542
$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
@@ -1144,4 +1144,56 @@ protected function getOutputContent(StreamOutput $output)
11441144

11451145
return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
11461146
}
1147+
1148+
public function testWithColspanAndMaxWith(): void
1149+
{
1150+
$table = new Table($output = $this->getOutputStream());
1151+
1152+
$table->setColumnMaxWidth(0, 15);
1153+
$table->setColumnMaxWidth(1, 15);
1154+
$table->setColumnMaxWidth(2, 15);
1155+
$table->setRows([
1156+
[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])],
1157+
new TableSeparator(),
1158+
[new TableCell('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor', ['colspan' => 3])],
1159+
new TableSeparator(),
1160+
[new TableCell('Lorem ipsum <fg=white;bg=red>dolor</> sit amet, consectetur ', ['colspan' => 2]), 'hello world'],
1161+
new TableSeparator(),
1162+
['hello <fg=white;bg=green>world</>', new TableCell('Lorem ipsum dolor sit amet, <fg=white;bg=green>consectetur</> adipiscing elit', ['colspan' => 2])],
1163+
new TableSeparator(),
1164+
['hello ', new TableCell('world', ['colspan' => 1]), 'Lorem ipsum dolor sit amet, consectetur'],
1165+
new TableSeparator(),
1166+
['Symfony ', new TableCell('Test', ['colspan' => 1]), 'Lorem <fg=white;bg=green>ipsum</> dolor sit amet, consectetur'],
1167+
])
1168+
;
1169+
$table->render();
1170+
1171+
$expected =
1172+
<<<TABLE
1173+
+-----------------+-----------------+-----------------+
1174+
| Lorem ipsum dolor sit amet, consectetur adipi |
1175+
| scing elit, sed do eiusmod tempor |
1176+
+-----------------+-----------------+-----------------+
1177+
| Lorem ipsum dolor sit amet, consectetur adipi |
1178+
| scing elit, sed do eiusmod tempor |
1179+
+-----------------+-----------------+-----------------+
1180+
| Lorem ipsum dolor sit amet, co | hello world |
1181+
| nsectetur | |
1182+
+-----------------+-----------------+-----------------+
1183+
| hello world | Lorem ipsum dolor sit amet, co |
1184+
| | nsectetur adipiscing elit |
1185+
+-----------------+-----------------+-----------------+
1186+
| hello | world | Lorem ipsum dol |
1187+
| | | or sit amet, co |
1188+
| | | nsectetur |
1189+
+-----------------+-----------------+-----------------+
1190+
| Symfony | Test | Lorem ipsum dol |
1191+
| | | or sit amet, co |
1192+
| | | nsectetur |
1193+
+-----------------+-----------------+-----------------+
1194+
1195+
TABLE;
1196+
1197+
$this->assertSame($expected, $this->getOutputContent($output));
1198+
}
11471199
}

0 commit comments

Comments
 (0)