feature #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 #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
This commit is contained in:
commit
21e50d59ca
@ -20,6 +20,7 @@ use Symfony\Component\Console\Exception\InvalidArgumentException;
|
|||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
* @author Саша Стаменковић <umpirsky@gmail.com>
|
* @author Саша Стаменковић <umpirsky@gmail.com>
|
||||||
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
||||||
|
* @author Max Grigorian <maxakawizard@gmail.com>
|
||||||
*/
|
*/
|
||||||
class Table
|
class Table
|
||||||
{
|
{
|
||||||
@ -61,6 +62,11 @@ class Table
|
|||||||
*/
|
*/
|
||||||
private $style;
|
private $style;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $columnStyles = array();
|
||||||
|
|
||||||
private static $styles;
|
private static $styles;
|
||||||
|
|
||||||
public function __construct(OutputInterface $output)
|
public function __construct(OutputInterface $output)
|
||||||
@ -139,6 +145,47 @@ class Table
|
|||||||
return $this->style;
|
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)
|
public function setHeaders(array $headers)
|
||||||
{
|
{
|
||||||
$headers = array_values($headers);
|
$headers = array_values($headers);
|
||||||
@ -308,12 +355,14 @@ class Table
|
|||||||
$width += strlen($cell) - mb_strwidth($cell, $encoding);
|
$width += strlen($cell) - mb_strwidth($cell, $encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$style = $this->getColumnStyle($column);
|
||||||
|
|
||||||
if ($cell instanceof TableSeparator) {
|
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 {
|
} else {
|
||||||
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
|
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
|
||||||
$content = sprintf($this->style->getCellRowContentFormat(), $cell);
|
$content = sprintf($style->getCellRowContentFormat(), $cell);
|
||||||
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->style->getPaddingChar(), $this->style->getPadType())));
|
$this->output->write(sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $style->getPadType())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -576,6 +576,36 @@ TABLE;
|
|||||||
| foo |
|
| 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 =
|
||||||
|
<<<TABLE
|
||||||
|
+---------------+----------------------+-----------------+--------+
|
||||||
|
| ISBN | Title | Author | Price |
|
||||||
|
+---------------+----------------------+-----------------+--------+
|
||||||
|
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
|
||||||
|
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
|
||||||
|
+---------------+----------------------+-----------------+--------+
|
||||||
|
|
||||||
TABLE;
|
TABLE;
|
||||||
|
|
||||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||||
|
Reference in New Issue
Block a user