From 668c50298271ce008ad15e6ad14ba008ab196ffd Mon Sep 17 00:00:00 2001 From: MAXakaWIZARD Date: Thu, 26 Mar 2015 21:58:49 +0200 Subject: [PATCH] [Console] [Helper] [Table] Add ability to set styles for individual columns --- .../Component/Console/Helper/Table.php | 55 ++++++++++++++++++- .../Console/Tests/Helper/TableTest.php | 30 ++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index fcb3b25a42..5d89b59a8b 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Exception\InvalidArgumentException; * @author Fabien Potencier * @author Саша Стаменковић * @author Abdellatif Ait boudad + * @author Max Grigorian */ class Table { @@ -61,6 +62,11 @@ class Table */ private $style; + /** + * @var array + */ + private $columnStyles = array(); + private static $styles; public function __construct(OutputInterface $output) @@ -139,6 +145,47 @@ class Table return $this->style; } + /** + * Sets table column style. + * + * @param int $columnIndex Column index + * @param TableStyle|string $name The style name or a TableStyle instance + * + * @return Table + */ + public function setColumnStyle($columnIndex, $name) + { + $columnIndex = intval($columnIndex); + + if ($name instanceof TableStyle) { + $this->columnStyles[$columnIndex] = $name; + } elseif (isset(self::$styles[$name])) { + $this->columnStyles[$columnIndex] = self::$styles[$name]; + } else { + throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + } + + return $this; + } + + /** + * Gets the current style for a column. + * + * If style was not set, it returns the global table style. + * + * @param int $columnIndex Column index + * + * @return TableStyle + */ + public function getColumnStyle($columnIndex) + { + if (isset($this->columnStyles[$columnIndex])) { + return $this->columnStyles[$columnIndex]; + } + + return $this->getStyle(); + } + public function setHeaders(array $headers) { $headers = array_values($headers); @@ -308,12 +355,14 @@ class Table $width += strlen($cell) - mb_strwidth($cell, $encoding); } + $style = $this->getColumnStyle($column); + if ($cell instanceof TableSeparator) { - $this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width))); + $this->output->write(sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width))); } else { $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell); - $content = sprintf($this->style->getCellRowContentFormat(), $cell); - $this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType()))); + $content = sprintf($style->getCellRowContentFormat(), $cell); + $this->output->write(sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType()))); } } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index de227949e9..b5c2eb11c5 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -576,6 +576,36 @@ TABLE; | foo | +---+--+ +TABLE; + + $this->assertEquals($expected, $this->getOutputContent($output)); + } + + public function testColumnStyle() + { + $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'), + )); + + $style = new TableStyle(); + $style->setPadType(STR_PAD_LEFT); + $table->setColumnStyle(3, $style); + + $table->render(); + + $expected = + <<assertEquals($expected, $this->getOutputContent($output));