Skip to content

Commit

Permalink
feature #50691 [Console] Aligned multiline text in vertical table (ja…
Browse files Browse the repository at this point in the history
…ytaph)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Console] Aligned multiline text in vertical table

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #50632
| License       | MIT
| Doc PR        | symfony/symfony-docs#...

When using a vertical table helper in the console, any multi-line (\n) text starts on a new line underneath the header. This fixes so the extra lines are aligned with the first line in the table.

Commits
-------

67fb325 [Console] Aligned multiline text in vertical table
  • Loading branch information
fabpot committed Jun 20, 2023
2 parents 3394330 + 67fb325 commit d48b929
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 d48b929

Please sign in to comment.