Skip to content

Commit

Permalink
bug #54001 [Console] Fix display of Table on Windows OS (VincentLanglet)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed Feb 20, 2024
2 parents 6438969 + 56e8692 commit 45c63b2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Symfony/Component/Console/Helper/Table.php
Expand Up @@ -621,9 +621,10 @@ private function buildTableRows(array $rows): TableRows
if (!strstr($cell ?? '', "\n")) {
continue;
}
$escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell)));
$eol = str_contains($cell ?? '', "\r\n") ? "\r\n" : "\n";
$escaped = implode($eol, array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode($eol, $cell)));
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default></>\n", $cell));
$lines = explode($eol, str_replace($eol, '<fg=default;bg=default></>'.$eol, $cell));
foreach ($lines as $lineKey => $line) {
if ($colspan > 1) {
$line = new TableCell($line, ['colspan' => $colspan]);
Expand Down Expand Up @@ -685,8 +686,9 @@ private function fillNextRows(array $rows, int $line): array
$nbLines = $cell->getRowspan() - 1;
$lines = [$cell];
if (strstr($cell, "\n")) {
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
$nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
$eol = str_contains($cell, "\r\n") ? "\r\n" : "\n";
$lines = explode($eol, str_replace($eol, '<fg=default;bg=default>'.$eol.'</>', $cell));
$nbLines = \count($lines) > $nbLines ? substr_count($cell, $eol) : $nbLines;

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

0 comments on commit 45c63b2

Please sign in to comment.