Skip to content

Commit 941075d

Browse files
committed
[Console] fixed column width when using the Table helper with some decoration in cells
1 parent 035e072 commit 941075d

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Helper/TableHelper.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ private function renderCell(array $row, $column, $cellFormat)
389389
$width += strlen($cell) - mb_strlen($cell, $encoding);
390390
}
391391

392+
$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);
393+
392394
$this->output->write(sprintf(
393395
$cellFormat,
394396
str_pad(
@@ -452,15 +454,7 @@ private function getColumnWidth($column)
452454
*/
453455
private function getCellWidth(array $row, $column)
454456
{
455-
if ($column < 0) {
456-
return 0;
457-
}
458-
459-
if (isset($row[$column])) {
460-
return $this->strlen($row[$column]);
461-
}
462-
463-
return $this->getCellWidth($row, $column - 1);
457+
return isset($row[$column]) ? $this->computeLengthWithoutDecoration($row[$column]) : 0;
464458
}
465459

466460
/**
@@ -472,6 +466,18 @@ private function cleanup()
472466
$this->numberOfColumns = null;
473467
}
474468

469+
private function computeLengthWithoutDecoration($string)
470+
{
471+
$formatter = $this->output->getFormatter();
472+
$isDecorated = $formatter->isDecorated();
473+
$formatter->setDecorated(false);
474+
475+
$string = $formatter->format($string);
476+
$formatter->setDecorated($isDecorated);
477+
478+
return $this->strlen($string);
479+
}
480+
475481
/**
476482
* {@inheritDoc}
477483
*/

Tests/Helper/TableHelperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,40 @@ public function testRenderProvider()
196196
TableHelper::LAYOUT_DEFAULT,
197197
'',
198198
),
199+
'Cell text with tags used for Output styling' => array(
200+
array('ISBN', 'Title', 'Author'),
201+
array(
202+
array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
203+
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
204+
),
205+
TableHelper::LAYOUT_DEFAULT,
206+
<<<TABLE
207+
+---------------+----------------------+-----------------+
208+
| ISBN | Title | Author |
209+
+---------------+----------------------+-----------------+
210+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
211+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
212+
+---------------+----------------------+-----------------+
213+
214+
TABLE
215+
),
216+
'Cell text with tags not used for Output styling' => array(
217+
array('ISBN', 'Title', 'Author'),
218+
array(
219+
array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
220+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
221+
),
222+
TableHelper::LAYOUT_DEFAULT,
223+
<<<TABLE
224+
+----------------------------------+----------------------+-----------------+
225+
| ISBN | Title | Author |
226+
+----------------------------------+----------------------+-----------------+
227+
| <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
228+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
229+
+----------------------------------+----------------------+-----------------+
230+
231+
TABLE
232+
),
199233
);
200234
}
201235

0 commit comments

Comments
 (0)