diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d4e0f9ba..df3764037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * added truncate method to FormatterHelper + * added setColumnWidth(s) method to Table 2.8.3 ----- diff --git a/Helper/Table.php b/Helper/Table.php index 1f103ad14..503210366 100644 --- a/Helper/Table.php +++ b/Helper/Table.php @@ -43,7 +43,7 @@ class Table * * @var array */ - private $columnWidths = array(); + private $effectiveColumnWidths = array(); /** * Number of columns cache. @@ -67,6 +67,13 @@ class Table */ private $columnStyles = array(); + /** + * User set column widths. + * + * @var array + */ + private $columnWidths = array(); + private static $styles; public function __construct(OutputInterface $output) @@ -186,6 +193,38 @@ public function getColumnStyle($columnIndex) return $this->getStyle(); } + /** + * Sets the minimum width of a column. + * + * @param int $columnIndex Column index + * @param int $width Minimum column width in characters + * + * @return Table + */ + public function setColumnWidth($columnIndex, $width) + { + $this->columnWidths[intval($columnIndex)] = intval($width); + + return $this; + } + + /** + * Sets the minimum width of all columns. + * + * @param array $widths + * + * @return Table + */ + public function setColumnWidths(array $widths) + { + $this->columnWidths = array(); + foreach ($widths as $index => $width) { + $this->setColumnWidth($index, $width); + } + + return $this; + } + public function setHeaders(array $headers) { $headers = array_values($headers); @@ -296,7 +335,7 @@ private function renderRowSeparator() $markup = $this->style->getCrossingChar(); for ($column = 0; $column < $count; ++$column) { - $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar(); + $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar(); } $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup)); @@ -342,11 +381,11 @@ private function renderRow(array $row, $cellFormat) private function renderCell(array $row, $column, $cellFormat) { $cell = isset($row[$column]) ? $row[$column] : ''; - $width = $this->columnWidths[$column]; + $width = $this->effectiveColumnWidths[$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->columnWidths[$nextColumn]; + $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn]; } } @@ -572,7 +611,7 @@ private function calculateColumnsWidth($rows) $lengths[] = $this->getCellWidth($row, $column); } - $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2; + $this->effectiveColumnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2; } } @@ -596,6 +635,8 @@ private function getColumnSeparatorWidth() */ private function getCellWidth(array $row, $column) { + $cellWidth = 0; + if (isset($row[$column])) { $cell = $row[$column]; $cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell); @@ -603,11 +644,11 @@ private function getCellWidth(array $row, $column) // we assume that cell value will be across more than one column. $cellWidth = $cellWidth / $cell->getColspan(); } - - return $cellWidth; } - return 0; + $columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0; + + return max($cellWidth, $columnWidth); } /** @@ -615,7 +656,7 @@ private function getCellWidth(array $row, $column) */ private function cleanup() { - $this->columnWidths = array(); + $this->effectiveColumnWidths = array(); $this->numberOfColumns = null; } diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index d917d6959..c8ab0316a 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -620,6 +620,69 @@ public function testColumnStyle() | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 | +---------------+----------------------+-----------------+--------+ +TABLE; + + $this->assertEquals($expected, $this->getOutputContent($output)); + } + + public function testColumnWith() + { + $table = new Table($output = $this->getOutputStream()); + $table + ->setHeaders(array('ISBN', 'Title', 'Author', 'Price')) + ->setRows(array( + array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'), + array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'), + )) + ->setColumnWidth(0, 15) + ->setColumnWidth(3, 10); + + $style = new TableStyle(); + $style->setPadType(STR_PAD_LEFT); + $table->setColumnStyle(3, $style); + + $table->render(); + + $expected = + <<