Skip to content

Commit

Permalink
[Console] Aligned multiline text in vertical table
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph authored and fabpot committed Jun 20, 2023
1 parent 3394330 commit 67fb325
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `SignalMap` to map signal value to its name
* Multi-line text in vertical tables is aligned properly

6.3
---
Expand Down
28 changes: 20 additions & 8 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,26 @@ public function render()
$maxRows = max(\count($headers), \count($row));
for ($i = 0; $i < $maxRows; ++$i) {
$cell = (string) ($row[$i] ?? '');
if ($headers && !$containsColspan) {
$rows[] = [sprintf(
'<comment>%s</>: %s',
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
$cell
)];
} elseif ('' !== $cell) {
$rows[] = [$cell];

$parts = explode("\n", $cell);
foreach ($parts as $idx => $part) {
if ($headers && !$containsColspan) {
if (0 === $idx) {
$rows[] = [sprintf(
'<comment>%s</>: %s',
str_pad($headers[$i] ?? '', $maxHeaderLength, ' ', \STR_PAD_LEFT),
$part
)];
} else {
$rows[] = [sprintf(
'%s %s',
str_pad('', $maxHeaderLength, ' ', \STR_PAD_LEFT),
$part
)];
}
} elseif ('' !== $cell) {
$rows[] = [$part];
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ public static function provideRenderVerticalTests(): \Traversable
|-------------------------|
| ISBN: 9971-5-0210-0 |
| Title: A Tale |
| of Two Cities |
| of Two Cities |
| Author: Charles Dickens |
| Price: 139.25 |
+-------------------------+
Expand Down

0 comments on commit 67fb325

Please sign in to comment.