8000 [Console] Aligned multiline text in vertical table · symfony/symfony@a01494a · GitHub
[go: up one dir, main page]

Skip to content

Commit a01494a

Browse files
committed
[Console] Aligned multiline text in vertical table
When using a vertical table helper in the console, any multiline (\n) text starts on a new line underneat the header. This fixes so the extra lines are aligned with the first line in the table.
1 parent 5bc63ad commit a01494a

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/Symfony/Component/Console/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Multi-line text in vertical tables is aligned properly
8+
49
6.3
510
---
611

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,26 @@ public function render()
364364
$maxRows = max(\count($headers), \count($row));
365365
for ($i = 0; $i < $maxRows; ++$i) {
366366
$cell = (string) ($row[$i] ?? '');
367-
if ($headers && !$containsColspan) {
368-
$rows[] = [sprintf(
369-
'<comment>%s</>: %s',
370-
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
371-
$cell
372-
)];
373-
} elseif ('' !== $cell) {
374-
$rows[] = [$cell];
367+
368+
$parts = explode("\n", $cell);
369+
foreach ($parts as $idx => $part) {
370+
if ($headers && !$containsColspan) {
371+
if (0 == $idx) {
372+
$rows[] = [sprintf(
373+
'<comment>%s</>: %s',
374+
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
375+
$part
376+
)];
377+
} else {
378+
$rows[] = [sprintf(
379+
'%s %s',
380+
str_pad('', $maxHeaderLength, ' ', \STR_PAD_LEFT),
381+
$part
382+
8000 )];
383+
}
384+
} elseif ('' !== $cell) {
385+
$rows[] = [$part];
386+
}
375387
}
376388
}
377389
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ public static function provideRenderVerticalTests(): \Traversable
17201720
|-------------------------|
17211721
| ISBN: 9971-5-0210-0 |
17221722
| Title: A Tale |
1723-
| of Two Cities |
1723+
| of Two Cities |
17241724
| Author: Charles Dickens |
17251725
| Price: 139.25 |
17261726
+-------------------------+

0 commit comments

Comments
 (0)
0