Skip to content
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
39 changes: 20 additions & 19 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,26 @@ public function setRow($column, array $row)
public function render()
{
$this->calculateNumberOfColumns();
$this->rows = $this->buildTableRows($this->rows);
$this->headers = $this->buildTableRows($this->headers);
$rows = $this->buildTableRows($this->rows);
$headers = $this->buildTableRows($this->headers);

$this->calculateColumnsWidth(array_merge($headers, $rows));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this method change the state of the object, shouldn't we make it return $columnWidths? To me, the render() method would be better not mutating $this, if that makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it could change the state of the object as we call cleanup on each call https://github.com/aitboudad/symfony/blob/colspan_render_table/src/Symfony/Component/Console/Helper/Table.php#L571
we need to pass $columnWidths and $numberOfColumns to many methods ? does it make sense ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed


$this->renderRowSeparator();
if (!empty($this->headers)) {
foreach ($this->headers as $header) {
if (!empty($headers)) {
foreach ($headers as $header) {
$this->renderRow($header, $this->style->getCellHeaderFormat());
$this->renderRowSeparator();
}
}
foreach ($this->rows as $row) {
foreach ($rows as $row) {
if ($row instanceof TableSeparator) {
$this->renderRowSeparator();
} else {
$this->renderRow($row, $this->style->getCellRowFormat());
}
}
if (!empty($this->rows)) {
if (!empty($rows)) {
$this->renderRowSeparator();
}

Expand All @@ -246,7 +248,7 @@ private function renderRowSeparator()

$markup = $this->style->getCrossingChar();
for ($column = 0; $column < $count; $column++) {
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->getColumnWidth($column)).$this->style->getCrossingChar();
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar();
}

$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
Expand Down Expand Up @@ -292,11 +294,11 @@ private function renderRow(array $row, $cellFormat)
private function renderCell(array $row, $column, $cellFormat)
{
$cell = isset($row[$column]) ? $row[$column] : '';
$width = $this->getColumnWidth($column);
$width = $this->columnWidths[$column];
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
// add the width of the following columns(numbers of colspan).
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
$width += $this->getColumnSeparatorWidth() + $this->getColumnWidth($nextColumn);
$width += $this->getColumnSeparatorWidth() + $this->columnWidths[$nextColumn];
}
}

Expand Down Expand Up @@ -509,21 +511,20 @@ private function getRowColumns($row)
*
* @return int
*/
private function getColumnWidth($column)
private function calculateColumnsWidth($rows)
{
if (isset($this->columnWidths[$column])) {
return $this->columnWidths[$column];
}
for ($column = 0; $column < $this->numberOfColumns; $column++) {
$lengths = array();
foreach ($rows as $row) {
if ($row instanceof TableSeparator) {
continue;
}

foreach (array_merge($this->headers, $this->rows) as $row) {
if ($row instanceof TableSeparator) {
continue;
$lengths[] = $this->getCellWidth($row, $column);
}

$lengths[] = $this->getCellWidth($row, $column);
$this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
}

return $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
}

/**
Expand Down
37 changes: 32 additions & 5 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ public function testRenderProvider()
array(
array('ISBN', 'Title', 'Author'),
array(
array("99921-58-10-7", "Divine\nComedy", "Dante Alighieri"),
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
array("9971-5-0210-2", "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
array("960-425-059-0", "The Lord of the Rings", "J. R. R.\nTolkien"),
array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
),
'default',
<<<TABLE
Expand Down Expand Up @@ -427,7 +427,7 @@ public function testRenderProvider()
array('ISBN', 'Author'),
array(
array(
new TableCell("9971-5-0210-0", array('rowspan' => 3, 'colspan' => 1)),
new TableCell('9971-5-0210-0', array('rowspan' => 3, 'colspan' => 1)),
'Dante Alighieri',
),
array(new TableSeparator()),
Expand Down Expand Up @@ -547,6 +547,33 @@ public function testRowSeparator()
| Bar3 |
+------+

TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
}

public function testRenderMultiCalls()
{
$table = new Table($output = $this->getOutputStream());
$table->setRows(array(
array(new TableCell('foo', array('colspan' => 2))),
));
$table->render();
$table->render();
$table->render();

$expected =
<<<TABLE
+---+--+
| foo |
+---+--+
+---+--+
| foo |
+---+--+
+---+--+
| foo |
+---+--+

TABLE;

$this->assertEquals($expected, $this->getOutputContent($output));
Expand Down