From 0ca23ea5e495cf16fd2a8217d89f1c17129ff59c Mon Sep 17 00:00:00 2001 From: Arjan Keeman Date: Thu, 11 Feb 2016 11:00:25 +0100 Subject: [PATCH 1/7] add column fixed width functionality + test --- .../Component/Console/Helper/Table.php | 58 ++++++++++++++++++- .../Console/Tests/Helper/TableTest.php | 32 ++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 1f103ad144ca5..c1502a2a38057 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -67,6 +67,13 @@ class Table */ private $columnStyles = array(); + /** + * Predefined fixed column widths. + * + * @var array + */ + private $columnFixedWidths = array(); + private static $styles; public function __construct(OutputInterface $output) @@ -186,6 +193,51 @@ public function getColumnStyle($columnIndex) return $this->getStyle(); } + /** + * Sets the fixed width for a column. + * + * If the width is set to 0, it will be reset to auto. + * + * @param int $columnIndex Column index + * @param int $width Column with in characters + * + * @return Table + */ + public function setColumnFixedWidth($columnIndex, $width) + { + $columnIndex = intval($columnIndex); + $width = intval($width); + + if (0 < $width) { + $this->columnFixedWidths[$columnIndex] = $width; + + } elseif (0 === $width) { + unset($this->columnFixedWidths[$columnIndex]); + + } else { + throw new InvalidArgumentException(sprintf('Width "%d" is not a valid column width.', $width)); + } + + return $this; + } + + /** + * Gets the column's declared fixed width. + * + * If no fixed width is set, it returns 0 and must be interpreted as auto. + * + * @param $columnIndex + * @return int + */ + public function getColumnFixedWidth($columnIndex) + { + if (isset($this->columnFixedWidths[$columnIndex])) { + return $this->columnFixedWidths[$columnIndex]; + } + + return 0; + } + public function setHeaders(array $headers) { $headers = array_values($headers); @@ -596,6 +648,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 +657,9 @@ 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; + return max($cellWidth, $this->getColumnFixedWidth($column)); } /** diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index d917d69591aa8..acfa60db2b2a6 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -620,6 +620,38 @@ 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 testColumnFixedWith() + { + $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'), + )) + ->setColumnFixedWidth(0, 15) + ->setColumnFixedWidth(3, 10); + + $style = new TableStyle(); + $style->setPadType(STR_PAD_LEFT); + $table->setColumnStyle(3, $style); + + $table->render(); + + $expected = + <<assertEquals($expected, $this->getOutputContent($output)); From 7f67cb1a5653a5ef27e2334e6ad4229e30a8715b Mon Sep 17 00:00:00 2001 From: Arjan Keeman Date: Mon, 15 Feb 2016 09:56:04 +0100 Subject: [PATCH 2/7] renamed columnWidth (cache) to effectiveColumnWidth; renamed columnFixedWidth to columnWidth; implemented setColumnWidths; implemented 'auto' as valid value --- .../Component/Console/Helper/Table.php | 77 +++++++++++-------- .../Console/Tests/Helper/TableTest.php | 37 ++++++++- 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index c1502a2a38057..681cef16b070d 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -43,7 +43,7 @@ class Table * * @var array */ - private $columnWidths = array(); + private $effectiveColumnWidths = array(); /** * Number of columns cache. @@ -68,11 +68,11 @@ class Table private $columnStyles = array(); /** - * Predefined fixed column widths. + * User set column widths. * * @var array */ - private $columnFixedWidths = array(); + private $columnWidths = array(); private static $styles; @@ -194,48 +194,63 @@ public function getColumnStyle($columnIndex) } /** - * Sets the fixed width for a column. - * - * If the width is set to 0, it will be reset to auto. + * Sets the width for a column. * - * @param int $columnIndex Column index - * @param int $width Column with in characters + * @param int $columnIndex Column index. + * @param int|string $width Column with in characters. Set to 'auto' to auto-fit the content. * * @return Table */ - public function setColumnFixedWidth($columnIndex, $width) + public function setColumnWidth($columnIndex, $width) { $columnIndex = intval($columnIndex); - $width = intval($width); - - if (0 < $width) { - $this->columnFixedWidths[$columnIndex] = $width; - } elseif (0 === $width) { - unset($this->columnFixedWidths[$columnIndex]); + if ('auto' !== $width) { + $width = intval($width); + } - } else { - throw new InvalidArgumentException(sprintf('Width "%d" is not a valid column width.', $width)); + if ('auto' !== $width && 0 >= $width) { + throw new InvalidArgumentException(sprintf( + 'Width "%d" is not a valid column width for column %d. Expected width > 0 or \'auto\'.', + $width, + $columnIndex + )); } + $this->columnWidths[$columnIndex] = $width; + return $this; } /** - * Gets the column's declared fixed width. + * Set all column widths. Use 'auto' to auto-fit the content. * - * If no fixed width is set, it returns 0 and must be interpreted as auto. + * @param array $widths * - * @param $columnIndex - * @return int + * @return Table */ - public function getColumnFixedWidth($columnIndex) + public function setColumnWidths(array $widths) { - if (isset($this->columnFixedWidths[$columnIndex])) { - return $this->columnFixedWidths[$columnIndex]; + $this->columnWidths = array(); + foreach ($widths as $index => $width) { + $this->setColumnWidth($index, $width); } - return 0; + return $this; + } + + /** + * Gets the column's declared width. + * + * If no width was set, it returns the default 'auto'. + * + * @param int $columnIndex + * + * @return int|string + */ + public function getColumnWidth($columnIndex) + { + return isset($this->columnWidths[$columnIndex]) ? $this->columnWidths[$columnIndex] : 'auto'; } public function setHeaders(array $headers) @@ -348,7 +363,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)); @@ -394,11 +409,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]; } } @@ -624,7 +639,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; } } @@ -659,7 +674,7 @@ private function getCellWidth(array $row, $column) } } - return max($cellWidth, $this->getColumnFixedWidth($column)); + return max($cellWidth, $this->getColumnWidth($column)); } /** @@ -667,7 +682,7 @@ private function getCellWidth(array $row, $column) */ private function cleanup() { - $this->columnWidths = array(); + $this->effectiveColumnWidths = array(); $this->numberOfColumns = null; } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index acfa60db2b2a6..71cbb19c94290 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -625,7 +625,7 @@ public function testColumnStyle() $this->assertEquals($expected, $this->getOutputContent($output)); } - public function testColumnFixedWith() + public function testColumnWith() { $table = new Table($output = $this->getOutputStream()); $table @@ -634,8 +634,39 @@ public function testColumnFixedWith() 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'), )) - ->setColumnFixedWidth(0, 15) - ->setColumnFixedWidth(3, 10); + ->setColumnWidth(0, 15) + ->setColumnWidth(3, 10); + + $style = new TableStyle(); + $style->setPadType(STR_PAD_LEFT); + $table->setColumnStyle(3, $style); + + $table->render(); + + $expected = + <<
assertEquals($expected, $this->getOutputContent($output)); + } + + public function testColumnWiths() + { + $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'), + )) + ->setColumnWidths(array(15, 'auto', 'auto', 10)); $style = new TableStyle(); $style->setPadType(STR_PAD_LEFT); From c6b528564852f53893c74ca2f7dfee2b45333561 Mon Sep 17 00:00:00 2001 From: Arjan keeman Date: Sun, 28 Feb 2016 11:29:17 +0100 Subject: [PATCH 3/7] added changelog note for Table's setColumnWidth(s) methods --- src/Symfony/Component/Console/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 6d4e0f9ba6b30..df37640375929 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * added truncate method to FormatterHelper + * added setColumnWidth(s) method to Table 2.8.3 ----- From 009b07c4778dcc958cbd348958c27d373c91ee37 Mon Sep 17 00:00:00 2001 From: Arjan keeman Date: Tue, 1 Mar 2016 15:53:42 +0100 Subject: [PATCH 4/7] remove getColumnWidth, added support for 0 and -1 values --- .../Component/Console/Helper/Table.php | 29 +++++-------------- .../Console/Tests/Helper/TableTest.php | 2 +- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 681cef16b070d..2205efe357aa1 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -207,16 +207,13 @@ public function setColumnWidth($columnIndex, $width) if ('auto' !== $width) { $width = intval($width); - } - if ('auto' !== $width && 0 >= $width) { - throw new InvalidArgumentException(sprintf( - 'Width "%d" is not a valid column width for column %d. Expected width > 0 or \'auto\'.', - $width, - $columnIndex - )); + if (-1 > $width) { + throw new InvalidArgumentException(sprintf('Width "%d" is not a valid column width for column %d. Expected width > 0 or "auto".',$width,$columnIndex)); + } } + $this->columnWidths[$columnIndex] = $width; return $this; @@ -239,20 +236,6 @@ public function setColumnWidths(array $widths) return $this; } - /** - * Gets the column's declared width. - * - * If no width was set, it returns the default 'auto'. - * - * @param int $columnIndex - * - * @return int|string - */ - public function getColumnWidth($columnIndex) - { - return isset($this->columnWidths[$columnIndex]) ? $this->columnWidths[$columnIndex] : 'auto'; - } - public function setHeaders(array $headers) { $headers = array_values($headers); @@ -674,7 +657,9 @@ private function getCellWidth(array $row, $column) } } - return max($cellWidth, $this->getColumnWidth($column)); + $columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0; + + return max($cellWidth, $columnWidth); } /** diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index 71cbb19c94290..b28c70efdd226 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -666,7 +666,7 @@ public function testColumnWiths() 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'), )) - ->setColumnWidths(array(15, 'auto', 'auto', 10)); + ->setColumnWidths(array(15, 'auto', -1, 10)); $style = new TableStyle(); $style->setPadType(STR_PAD_LEFT); From 98d7858c87cf346d602db01cb70aeb7609cd853f Mon Sep 17 00:00:00 2001 From: Arjan keeman Date: Tue, 1 Mar 2016 16:02:03 +0100 Subject: [PATCH 5/7] remove support for "auto" --- src/Symfony/Component/Console/Helper/Table.php | 18 +++++------------- .../Console/Tests/Helper/TableTest.php | 2 +- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 2205efe357aa1..a630b152b0d38 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -194,25 +194,17 @@ public function getColumnStyle($columnIndex) } /** - * Sets the width for a column. + * Sets the minimum width of a column. * - * @param int $columnIndex Column index. - * @param int|string $width Column with in characters. Set to 'auto' to auto-fit the content. + * @param int $columnIndex Column index. + * @param int $width Column minimum with in characters. * * @return Table */ public function setColumnWidth($columnIndex, $width) { $columnIndex = intval($columnIndex); - - if ('auto' !== $width) { - $width = intval($width); - - if (-1 > $width) { - throw new InvalidArgumentException(sprintf('Width "%d" is not a valid column width for column %d. Expected width > 0 or "auto".',$width,$columnIndex)); - } - } - + $width = intval($width); $this->columnWidths[$columnIndex] = $width; @@ -220,7 +212,7 @@ public function setColumnWidth($columnIndex, $width) } /** - * Set all column widths. Use 'auto' to auto-fit the content. + * Set the minimum width of all columns. * * @param array $widths * diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index b28c70efdd226..c8ab0316acce1 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -666,7 +666,7 @@ public function testColumnWiths() 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'), )) - ->setColumnWidths(array(15, 'auto', -1, 10)); + ->setColumnWidths(array(15, 0, -1, 10)); $style = new TableStyle(); $style->setPadType(STR_PAD_LEFT); From 45cb216480216d05db19da3c736016622d542736 Mon Sep 17 00:00:00 2001 From: Arjan keeman Date: Tue, 1 Mar 2016 18:06:05 +0100 Subject: [PATCH 6/7] inline intvals --- src/Symfony/Component/Console/Helper/Table.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index a630b152b0d38..65837edeb2398 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -203,10 +203,7 @@ public function getColumnStyle($columnIndex) */ public function setColumnWidth($columnIndex, $width) { - $columnIndex = intval($columnIndex); - $width = intval($width); - - $this->columnWidths[$columnIndex] = $width; + $this->columnWidths[intval($columnIndex)] = intval($width); return $this; } From 0132ba76fa6bb31d620edf8c461591cfdf55652b Mon Sep 17 00:00:00 2001 From: Arjan keeman Date: Tue, 1 Mar 2016 18:29:27 +0100 Subject: [PATCH 7/7] remove dots at the end of an @param, set -> sets, widt -> width --- src/Symfony/Component/Console/Helper/Table.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index 65837edeb2398..5032103661148 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -196,8 +196,8 @@ public function getColumnStyle($columnIndex) /** * Sets the minimum width of a column. * - * @param int $columnIndex Column index. - * @param int $width Column minimum with in characters. + * @param int $columnIndex Column index + * @param int $width Minimum column width in characters * * @return Table */ @@ -209,7 +209,7 @@ public function setColumnWidth($columnIndex, $width) } /** - * Set the minimum width of all columns. + * Sets the minimum width of all columns. * * @param array $widths *