Skip to content

Commit 208e6b0

Browse files
committed
bug symfony#18774 [console][table] adjust width of colspanned cell. (aitboudad)
This PR was merged into the 2.8 branch. Discussion ---------- [console][table] adjust width of colspanned cell. | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18627 | License | MIT | Doc PR | ~ Commits ------- 081ad73 [console][table] adjust width of colspanned cell.
2 parents 1253b1d + 081ad73 commit 208e6b0

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,18 @@ private function calculateColumnsWidth($rows)
569569
continue;
570570
}
571571

572+
foreach ($row as $i => $cell) {
573+
if ($cell instanceof TableCell) {
574+
$textLength = strlen($cell);
575+
if ($textLength > 0) {
576+
$contentColumns = str_split($cell, ceil($textLength / $cell->getColspan()));
577+
foreach ($contentColumns as $position => $content) {
578+
$row[$i + $position] = $content;
579+
}
580+
}
581+
}
582+
}
583+
572584
$lengths[] = $this->getCellWidth($row, $column);
573585
}
574586

@@ -599,10 +611,6 @@ private function getCellWidth(array $row, $column)
599611
if (isset($row[$column])) {
600612
$cell = $row[$column];
601613
$cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
602-
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
603-
// we assume that cell value will be across more than one column.
604-
$cellWidth = $cellWidth / $cell->getColspan();
605-
}
606614

607615
return $cellWidth;
608616
}

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,27 @@ public function testRenderProvider()
269269
'9971-5-0210-0',
270270
new TableCell("A Tale of \nTwo Cities", array('colspan' => 2)),
271271
),
272+
new TableSeparator(),
273+
array(
274+
new TableCell('Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil!', array('colspan' => 3)),
275+
),
272276
),
273277
'default',
274278
<<<TABLE
275-
+----------------+---------------+-----------------+
276-
| ISBN | Title | Author |
277-
+----------------+---------------+-----------------+
278-
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
279-
+----------------+---------------+-----------------+
280-
| Divine Comedy(Dante Alighieri) |
281-
+----------------+---------------+-----------------+
282-
| Arduino: A Quick-Start Guide | Mark Schmidt |
283-
+----------------+---------------+-----------------+
284-
| 9971-5-0210-0 | A Tale of |
285-
| | Two Cities |
286-
+----------------+---------------+-----------------+
279+
+-------------------------------+-------------------------------+-----------------------------+
280+
| ISBN | Title | Author |
281+
+-------------------------------+-------------------------------+-----------------------------+
282+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
283+
+-------------------------------+-------------------------------+-----------------------------+
284+
| Divine Comedy(Dante Alighieri) |
285+
+-------------------------------+-------------------------------+-----------------------------+
286+
| Arduino: A Quick-Start Guide | Mark Schmidt |
287+
+-------------------------------+-------------------------------+-----------------------------+
288+
| 9971-5-0210-0 | A Tale of |
289+
| | Two Cities |
290+
+-------------------------------+-------------------------------+-----------------------------+
291+
| Cupiditate dicta atque porro, tempora exercitationem modi animi nulla nemo vel nihil! |
292+
+-------------------------------+-------------------------------+-----------------------------+
287293
288294
TABLE
289295
),
@@ -336,16 +342,16 @@ public function testRenderProvider()
336342
),
337343
'default',
338344
<<<TABLE
339-
+------------------+--------+-----------------+
340-
| ISBN | Title | Author |
341-
+------------------+--------+-----------------+
342-
| 9971-5-0210-0 | Dante Alighieri |
343-
| | Charles Dickens |
344-
+------------------+--------+-----------------+
345-
| Dante Alighieri | 9971-5-0210-0 |
346-
| J. R. R. Tolkien | |
347-
| J. R. R | |
348-
+------------------+--------+-----------------+
345+
+------------------+---------+-----------------+
346+
| ISBN | Title | Author |
347+
+------------------+---------+-----------------+
348+
| 9971-5-0210-0 | Dante Alighieri |
349+
| | Charles Dickens |
350+
+------------------+---------+-----------------+
351+
| Dante Alighieri | 9971-5-0210-0 |
352+
| J. R. R. Tolkien | |
353+
| J. R. R | |
354+
+------------------+---------+-----------------+
349355
350356
TABLE
351357
),
@@ -473,9 +479,9 @@ public function testRenderProvider()
473479
),
474480
'default',
475481
<<<TABLE
476-
+--+--+--+--+--+--+--+--+--+
477-
| 1 | 2 | 3 | 4 |
478-
+--+--+--+--+--+--+--+--+--+
482+
+---+--+--+---+--+---+--+---+--+
483+
| 1 | 2 | 3 | 4 |
484+
+---+--+--+---+--+---+--+---+--+
479485
480486
TABLE
481487
),
@@ -580,15 +586,15 @@ public function testRenderMultiCalls()
580586

581587
$expected =
582588
<<<TABLE
583-
+---+--+
584-
| foo |
585-
+---+--+
586-
+---+--+
587-
| foo |
588-
+---+--+
589-
+---+--+
590-
| foo |
591-
+---+--+
589+
+----+---+
590+
| foo |
591+
+----+---+
592+
+----+---+
593+
| foo |
594+
+----+---+
595+
+----+---+
596+
| foo |
597+
+----+---+
592598
593599
TABLE;
594600

0 commit comments

Comments
 (0)