Skip to content

Commit ddd3620

Browse files
committed
[Console] [Helper] [Table] Add ability to set styles for individual columns
1 parent 350f30b commit ddd3620

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

src/Symfony/Component/Console/Helper/Table.php

+60-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author Fabien Potencier <fabien@symfony.com>
2020
* @author Саша Стаменковић <umpirsky@gmail.com>
2121
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
22+
* @author Max Grigorian <maxakawizard@gmail.com>
2223
*/
2324
class Table
2425
{
@@ -60,6 +61,11 @@ class Table
6061
*/
6162
private $style;
6263

64+
/**
65+
* @var array
66+
*/
67+
private $columnStyles = array();
68+
6369
private static $styles;
6470

6571
public function __construct(OutputInterface $output)
@@ -138,6 +144,55 @@ public function getStyle()
138144
return $this->style;
139145
}
140146

147+
/**
148+
* Sets table style.
149+
*
150+
* @param int $columnIndex Column index
151+
* @param TableStyle|string $name The style name or a TableStyle instance
152+
*
153+
* @return Table
154+
*/
155+
public function setColumnStyle($columnIndex, $name)
156+
{
157+
$isIndexValid = false;
158+
if (is_int($columnIndex)) {
159+
$isIndexValid = true;
160+
} elseif (is_string($columnIndex)) {
161+
$isIndexValid = ctype_digit($columnIndex);
162+
}
163+
164+
if (!$isIndexValid) {
165+
throw new \InvalidArgumentException(sprintf('Invalid column index: "%s".', $columnIndex));
166+
}
167+
168+
$columnIndex = intval($columnIndex);
169+
170+
if ($name instanceof TableStyle) {
171+
$this->columnStyles[$columnIndex] = $name;
172+
} elseif (isset(self::$styles[$name])) {
173+
$this->columnStyles[$columnIndex] = self::$styles[$name];
174+
} else {
175+
throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
176+
}
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* Gets the current style for specified column.
183+
*
184+
* @var int $columnIndex Column index
185+
* @return TableStyle
186+
*/
187+
public function getColumnStyle($columnIndex)
188+
{
189+
if (isset($this->columnStyles[$columnIndex])) {
190+
return $this->columnStyles[$columnIndex];
191+
}
192+
193+
return $this->getStyle();
194+
}
195+
141196
public function setHeaders(array $headers)
142197
{
143198
$headers = array_values($headers);
@@ -305,12 +360,14 @@ private function renderCell(array $row, $column, $cellFormat)
305360
$width += strlen($cell) - mb_strwidth($cell, $encoding);
306361
}
307362

363+
$style = $this->getColumnStyle($column);
364+
308365
if ($cell instanceof TableSeparator) {
309-
$this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width)));
366+
$this->output->write(sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width)));
310367
} else {
311368
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
312-
$content = sprintf($this->style->getCellRowContentFormat(), $cell);
313-
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType())));
369+
$content = sprintf($style->getCellRowContentFormat(), $cell);
370+
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType())));
314371
}
315372
}
316373

src/Symfony/Component/Console/Tests/Helper/TableTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,36 @@ public function testRowSeparator()
547547
| Bar3 |
548548
+------+
549549
550+
TABLE;
551+
552+
$this->assertEquals($expected, $this->getOutputContent($output));
553+
}
554+
555+
public function testColumnStyle()
556+
{
557+
$table = new Table($output = $this->getOutputStream());
558+
$table
559+
->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
560+
->setRows(array(
561+
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
562+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
563+
));
564+
565+
$style = new TableStyle();
566+
$style->setPadType(STR_PAD_LEFT);
567+
$table->setColumnStyle(3, $style);
568+
569+
$table->render();
570+
571+
$expected =
572+
<<<TABLE
573+
+---------------+----------------------+-----------------+--------+
574+
| ISBN | Title | Author | Price |
575+
+---------------+----------------------+-----------------+--------+
576+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
577+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
578+
+---------------+----------------------+-----------------+--------+
579+
550580
TABLE;
551581

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

0 commit comments

Comments
 (0)