Skip to content

Commit d62b765

Browse files
committed
bug symfony#22217 [Console] Fix table cell styling (ro0NL)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix table cell styling | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | tiny one | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> Fixes an issue with newlines and table cells. Remembered this little trick from @chalasr as we had it before with style blocks i believe.. ```php $table = new Table($output); $table->setRows(array( array(new TableCell('<error>Dont break'."\n".'here</error>', array('colspan' => 2))), new TableSeparator(), array('foo', new TableCell('<error>Dont break'."\n".'here</error>', array('rowspan' => 2))), array('bar'), )); $table->render(); ``` Before ![image](https://cloud.githubusercontent.com/assets/1047696/24467857/74dacc9e-14b6-11e7-8f62-3831508ac949.png) After ![image](https://cloud.githubusercontent.com/assets/1047696/24467923/bb578f0e-14b6-11e7-85ed-039cd73b81a0.png) Commits ------- 53ecf83 [Console] Fix table cell styling
2 parents fb6de49 + 53ecf83 commit d62b765

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public static function formatMemory($memory)
109109
}
110110

111111
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
112+
{
113+
return self::strlen(self::removeDecoration($formatter, $string));
114+
}
115+
116+
public static function removeDecoration(OutputFormatterInterface $formatter, $string)
112117
{
113118
$isDecorated = $formatter->isDecorated();
114119
$formatter->setDecorated(false);
@@ -118,6 +123,6 @@ public static function strlenWithoutDecoration(OutputFormatterInterface $formatt
118123
$string = preg_replace("/\033\[[^m]*m/", '', $string);
119124
$formatter->setDecorated($isDecorated);
120125

121-
return self::strlen($string);
126+
return $string;
122127
}
123128
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private function buildTableRows($rows)
341341
if (!strstr($cell, "\n")) {
342342
continue;
343343
}
344-
$lines = explode("\n", $cell);
344+
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
345345
foreach ($lines as $lineKey => $line) {
346346
if ($cell instanceof TableCell) {
347347
$line = new TableCell($line, array('colspan' => $cell->getColspan()));
@@ -382,7 +382,7 @@ private function fillNextRows($rows, $line)
382382
$nbLines = $cell->getRowspan() - 1;
383383
$lines = array($cell);
384384
if (strstr($cell, "\n")) {
385-
$lines = explode("\n", $cell);
385+
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell));
386386
$nbLines = count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines;
387387

388388
$rows[$line][$column] = new TableCell($lines[0], array('colspan' => $cell->getColspan()));
@@ -514,16 +514,19 @@ private function getColumnWidth($column)
514514
return $this->columnWidths[$column];
515515
}
516516

517+
$lengths = array();
518+
517519
foreach (array_merge($this->headers, $this->rows) as $row) {
518520
if ($row instanceof TableSeparator) {
519521
continue;
520522
}
521523

522524
foreach ($row as $i => $cell) {
523525
if ($cell instanceof TableCell) {
524-
$textLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
526+
$textContent = Helper::removeDecoration($this->output->getFormatter(), $cell);
527+
$textLength = Helper::strlen($textContent);
525528
if ($textLength > 0) {
526-
$contentColumns = str_split($cell, ceil($textLength / $cell->getColspan()));
529+
$contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan()));
527530
foreach ($contentColumns as $position => $content) {
528531
$row[$i + $position] = $content;
529532
}

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,35 @@ public function renderProvider()
511511
| Dante Alighieri | J. R. R. Tolkien | J. R. R |
512512
+-----------------+------------------+---------+
513513
514+
TABLE
515+
,
516+
true,
517+
),
518+
'Row with formatted cells containing a newline' => array(
519+
array(),
520+
array(
521+
array(
522+
new TableCell('<error>Dont break'."\n".'here</error>', array('colspan' => 2)),
523+
),
524+
new TableSeparator(),
525+
array(
526+
'foo',
527+
new TableCell('<error>Dont break'."\n".'here</error>', array('rowspan' => 2)),
528+
),
529+
array(
530+
'bar',
531+
),
532+
),
533+
'default',
534+
<<<'TABLE'
535+
+-------+------------+
536+
| Dont break |
537+
| here |
538+
+-------+------------+
539+
| foo | Dont break |
540+
| bar | here |
541+
+-------+------------+
542+
514543
TABLE
515544
,
516545
true,

0 commit comments

Comments
 (0)