Skip to content

Commit

Permalink
bug #30911 [Console] Fix table trailing backslash (maidmaid)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.2 branch.

Discussion
----------

[Console] Fix table trailing backslash

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30725
| License       | MIT
| Doc PR        | /

```php
(new Table($output))
    ->setColumnMaxWidth(0, 5)
    ->setRows([['1234\6']])
    ->render();
;
```
before:

```
+-------+
| 1234<fg=default;bg=default> |
| 6     |
+-------+
```

after:

```
+-------+
| 1234\ |
| 6     |
+-------+
```

#EUFOSSA

Commits
-------

48f020f Fix table trailing backslash
  • Loading branch information
fabpot committed Apr 6, 2019
2 parents a815308 + 48f020f commit b46ca51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Symfony/Component/Console/Helper/Table.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\WrappableOutputFormatterInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -528,6 +529,8 @@ private function buildTableRows($rows)
if (!strstr($cell, "\n")) {
continue;
}
$escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell)));
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
foreach ($lines as $lineKey => $line) {
if ($cell instanceof TableCell) {
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Expand Up @@ -1072,6 +1072,26 @@ public function testColumnMaxWidths()
| | ities | | |
+---------------+-------+------------+-----------------+
TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
}

public function testColumnMaxWidthsWithTrailingBackslash()
{
(new Table($output = $this->getOutputStream()))
->setColumnMaxWidth(0, 5)
->setRows([['1234\6']])
->render()
;

$expected =
<<<'TABLE'
+-------+
| 1234\ |
| 6 |
+-------+
TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
Expand Down

0 comments on commit b46ca51

Please sign in to comment.