Skip to content

Commit

Permalink
[Console] Aligned multiline text in vertical table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jaytaph committed Jun 17, 2023
1 parent 5bc63ad commit a96e28e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* 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
88 changes: 44 additions & 44 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ public static function renderProvider()
$books,
'compact',
<<<'TABLE'
ISBN Title Author
99921-58-10-7 Divine Comedy Dante Alighieri
9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
ISBN Title Author
99921-58-10-7 Divine Comedy Dante Alighieri
9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
TABLE
],
Expand All @@ -132,14 +132,14 @@ public static function renderProvider()
$books,
'borderless',
<<<'TABLE'
=============== ========================== ==================
ISBN Title Author
=============== ========================== ==================
99921-58-10-7 Divine Comedy Dante Alighieri
9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
=============== ========================== ==================
=============== ========================== ==================
ISBN Title Author
=============== ========================== ==================
99921-58-10-7 Divine Comedy Dante Alighieri
9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
=============== ========================== ==================
TABLE
],
Expand Down 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 Expand Up @@ -1821,17 +1821,17 @@ public static function provideRenderVerticalTests(): \Traversable

yield 'Borderless style' => [
<<<EOTXT
==============================
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
==============================
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
==============================
==============================
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
==============================
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
==============================
EOTXT
,
Expand All @@ -1842,15 +1842,15 @@ public static function provideRenderVerticalTests(): \Traversable

yield 'Compact style' => [
<<<EOTXT
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
EOTXT
,
Expand All @@ -1861,17 +1861,17 @@ public static function provideRenderVerticalTests(): \Traversable

yield 'symfony-style-guide style' => [
<<<EOTXT
------------------------------
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
------------------------------
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
------------------------------
------------------------------
ISBN: 99921-58-10-7
Title: Divine Comedy
Author: Dante Alighieri
Price: 9.95
------------------------------
ISBN: 9971-5-0210-0
Title: A Tale of Two Cities
Author: Charles Dickens
Price: 139.25
------------------------------
EOTXT
,
Expand Down

0 comments on commit a96e28e

Please sign in to comment.