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

[Console] fixed column width when using the Table helper with some decoration in cells #9889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions 24 src/Symfony/Component/Console/Helper/TableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ private function renderCell(array $row, $column, $cellFormat)
$width += strlen($cell) - mb_strlen($cell, $encoding);
}

$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);

$this->output->write(sprintf(
$cellFormat,
str_pad(
Expand Down Expand Up @@ -452,15 +454,7 @@ private function getColumnWidth($column)
*/
private function getCellWidth(array $row, $column)
{
if ($column < 0) {
return 0;
}

if (isset($row[$column])) {
return $this->strlen($row[$column]);
}

return $this->getCellWidth($row, $column - 1);
return isset($row[$column]) ? $this->computeLengthWithoutDecoration($row[$column]) : 0;
}

/**
Expand All @@ -472,6 +466,18 @@ private function cleanup()
$this->numberOfColumns = null;
}

private function computeLengthWithoutDecoration($string)
{
$formatter = $this->output->getFormatter();
$isDecorated = $formatter->isDecorated();
$formatter->setDecorated(false);

$string = $formatter->format($string);
$formatter->setDecorated($isDecorated);

return $this->strlen($string);
}

/**
* {@inheritDoc}
*/
Expand Down
34 changes: 34 additions & 0 deletions 34 src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,40 @@ public function testRenderProvider()
TableHelper::LAYOUT_DEFAULT,
'',
),
'Cell text with tags used for Output styling' => array(
array('ISBN', 'Title', 'Author'),
array(
array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
),
TableHelper::LAYOUT_DEFAULT,
<<<TABLE
+---------------+----------------------+-----------------+
| ISBN | Title | Author |
+---------------+----------------------+-----------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
+---------------+----------------------+-----------------+

TABLE
),
'Cell text with tags not used for Output styling' => array(
array('ISBN', 'Title', 'Author'),
array(
array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
),
TableHelper::LAYOUT_DEFAULT,
<<<TABLE
+----------------------------------+----------------------+-----------------+
| ISBN | Title | Author |
+----------------------------------+----------------------+-----------------+
| <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
+----------------------------------+----------------------+-----------------+

TABLE
),
);
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.