Skip to content

Commit 21e50d5

Browse files
committed
feature symfony#14044 [Console] [Helper] [Table] Columns styles (MAXakaWIZARD)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes symfony#14044). Discussion ---------- [Console] [Helper] [Table] Columns styles | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This PR introduces ability to set styles for individual columns in table. For example, we can apply STR_PAD_LEFT for the last column (useful for money, file size etc). Code: ```php use Symfony\Component\Finder\Finder; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableStyle; $table = new Table($output); $table->setHeaders(['#', 'Path', 'Size']); $style = new TableStyle(); $style->setPadType(STR_PAD_LEFT); $table->setColumnStyle(2, $style); $finder = new Finder(); $finder->files()->in("/path/to/dir")->name('*.php') $counter = 0; foreach ($finder as $file) { $counter++; $table->addRow([$counter, $file->getRealPath(), number_format($file->getSize(), 0, '.', ' ')]); } $table->render(); ``` Output: ``` +---+---------------------+--------+ | # | Path | Size | +---+---------------------+--------+ | 1 | autoload.php | 183 | | 2 | ApplicationTest.php | 47 794 | | 3 | CommandTest.php | 14 965 | | 4 | ListCommandTest.php | 2 369 | +---+---------------------+--------+ ``` Commits ------- 668c502 [Console] [Helper] [Table] Add ability to set styles for individual columns
2 parents b6ab750 + 668c502 commit 21e50d5

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @author Fabien Potencier <fabien@symfony.com>
2121
* @author Саша Стаменковић <umpirsky@gmail.com>
2222
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
23+
* @author Max Grigorian <maxakawizard@gmail.com>
2324
*/
2425
class Table
2526
{
@@ -61,6 +62,11 @@ class Table
6162
*/
6263
private $style;
6364

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

6672
public function __construct(OutputInterface $output)
@@ -139,6 +145,47 @@ public function getStyle()
139145
return $this->style;
140146
}
141147

148+
/**
149+
* Sets table column style.
150+
*
151+
* @param int $columnIndex Column index
152+
* @param TableStyle|string $name The style name or a TableStyle instance
153+
*
154+
* @return Table
155+
*/
156+
public function setColumnStyle($columnIndex, $name)
157+
{
158+
$columnIndex = intval($columnIndex);
159+
160+
if ($name instanceof TableStyle) {
161+
$this->columnStyles[$columnIndex] = $name;
162+
} elseif (isset(self::$styles[$name])) {
163+
$this->columnStyles[$columnIndex] = self::$styles[$name];
164+
} else {
165+
throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
166+
}
167+
168+
return $this;
169+
}
170+
171+
/**
172+
* Gets the current style for a column.
173+
*
174+
* If style was not set, it returns the global table style.
175+
*
176+
* @param int $columnIndex Column index
177+
*
178+
* @return TableStyle
179+
*/
180+
public function getColumnStyle($columnIndex)
181+
{
182+
if (isset($this->columnStyles[$columnIndex])) {
183+
return $this->columnStyles[$columnIndex];
184+
}
185+
186+
return $this->getStyle();
187+
}
188+
142189
public function setHeaders(array $headers)
143190
{
144191
$headers = array_values($headers);
@@ -308,12 +355,14 @@ private function renderCell(array $row, $column, $cellFormat)
308355
$width += strlen($cell) - mb_strwidth($cell, $encoding);
309356
}
310357

358+
$style = $this->getColumnStyle($column);
359+
311360
if ($cell instanceof TableSeparator) {
312-
$this->output->write(sprintf($this->style->getBorderFormat(), str_repeat($this->style->getHorizontalBorderChar(), $width)));
361+
$this->output->write(sprintf($style->getBorderFormat(), str_repeat($style->getHorizontalBorderChar(), $width)));
313362
} else {
314363
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
315-
$content = sprintf($this->style->getCellRowContentFormat(), $cell);
316-
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType())));
364+
$content = sprintf($style->getCellRowContentFormat(), $cell);
365+
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType())));
317366
}
318367
}
319368

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,36 @@ public function testRenderMultiCalls()
576576
| foo |
577577
+---+--+
578578
579+
TABLE;
580+
581+
$this->assertEquals($expected, $this->getOutputContent($output));
582+
}
583+
584+
public function testColumnStyle()
585+
{
586+
$table = new Table($output = $this->getOutputStream());
587+
$table
588+
->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
589+
->setRows(array(
590+
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
591+
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
592+
));
593+
594+
$style = new TableStyle();
595+
$style->setPadType(STR_PAD_LEFT);
596+
$table->setColumnStyle(3, $style);
597+
598+
$table->render();
599+
600+
$expected =
601+
<<<TABLE
602+
+---------------+----------------------+-----------------+--------+
603+
| ISBN | Title | Author | Price |
604+
+---------------+----------------------+-----------------+--------+
605+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
606+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
607+
+---------------+----------------------+-----------------+--------+
608+
579609
TABLE;
580610

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy