Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d41742

Browse filesBrowse files
committed
bug #9889 [Console] fixed column width when using the Table helper with some decoration in cells (fabpot)
This PR was merged into the 2.3 branch. Discussion ---------- [Console] fixed column width when using the Table helper with some decoration in cells | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8152, #9366 | License | MIT | Doc PR | n/a This PR fixes the same issue as #9366 but works in all situations (all kind of styles, when the string is shorter than any other one or larger than any other ones, ...). I'm not very satisfied with the fix and especially the `computeLengthWithoutDecoration` method, but the whole helper should be rethought to make it stateless (out of the scope of this PR). Commits ------- 5b4d057 [Console] fixed column width when using the Table helper with some decoration in cells
2 parents e0126d9 + 5b4d057 commit 6d41742
Copy full SHA for 6d41742

File tree

Expand file treeCollapse file tree

2 files changed

+49
-9
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+49
-9
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/TableHelper.php
+15-9Lines 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
*/

‎src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php
+34Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.