Skip to content

Commit 45c63b2

Browse files
bug symfony#54001 [Console] Fix display of Table on Windows OS (VincentLanglet)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Console] Fix display of Table on Windows OS | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT When running a console on a Windows environment lines endings are `\r\n` instead of `\n`. In the Table, when adding space at the end of the line, all the line manipulations are done on `\n` which breaks line endings regularly changing `\r\n` to `\r (lot of spaces) \n` instead of `(lot of spaces) \r\n`. Commits ------- 56e8692 [Console] Fix display of Table on Windows OS
2 parents 6438969 + 56e8692 commit 45c63b2

File tree

1 file changed

+6
-4
lines changed
  • src/Symfony/Component/Console/Helper

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,10 @@ private function buildTableRows(array $rows): TableRows
621621
if (!strstr($cell ?? '', "\n")) {
622622
continue;
623623
}
624-
$escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell)));
624+
$eol = str_contains($cell ?? '', "\r\n") ? "\r\n" : "\n";
625+
$escaped = implode($eol, array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode($eol, $cell)));
625626
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
626-
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default></>\n", $cell));
627+
$lines = explode($eol, str_replace($eol, '<fg=default;bg=default></>'.$eol, $cell));
627628
foreach ($lines as $lineKey => $line) {
628629
if ($colspan > 1) {
629630
$line = new TableCell($line, ['colspan' => $colspan]);
@@ -685,8 +686,9 @@ private function fillNextRows(array $rows, int $line): array
685686
$nbLines = $cell->getRowspan() - 1;
686687
$lines = [$cell];
687688
if (strstr($cell, "\n")) {
688-
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
689-
$nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
689+
$eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
690+
$lines = explode($eol, str_replace($eol, '<fg=default;bg=default>'.$eol.'</>', $cell));
691+
$nbLines = \count($lines) > $nbLines ? substr_count($cell, $eol) : $nbLines;
690692

691693
$rows[$line][$column] = new TableCell($lines[0], ['colspan' => $cell->getColspan(), 'style' => $cell->getStyle()]);
692694
unset($lines[0]);

0 commit comments

Comments
 (0)