Created new table with row separators for CLI, to use with multi-line rows

This commit is contained in:
Alejandro Celaya
2021-08-03 10:21:42 +02:00
parent 8fbf05acd4
commit 20f70b8b07
10 changed files with 40 additions and 12 deletions

View File

@@ -5,20 +5,33 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Util;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Output\OutputInterface;
use function Functional\intersperse;
final class ShlinkTable
{
private const DEFAULT_STYLE_NAME = 'default';
private const TABLE_TITLE_STYLE = '<options=bold> %s </>';
public function __construct(private Table $baseTable)
private function __construct(private Table $baseTable, private bool $withRowSeparators)
{
}
public static function fromOutput(OutputInterface $output): self
public static function default(OutputInterface $output): self
{
return new self(new Table($output));
return new self(new Table($output), false);
}
public static function withRowSeparators(OutputInterface $output): self
{
return new self(new Table($output), true);
}
public static function fromBaseTable(Table $baseTable): self
{
return new self($baseTable, false);
}
public function render(array $headers, array $rows, ?string $footerTitle = null, ?string $headerTitle = null): void
@@ -26,11 +39,12 @@ final class ShlinkTable
$style = Table::getStyleDefinition(self::DEFAULT_STYLE_NAME);
$style->setFooterTitleFormat(self::TABLE_TITLE_STYLE)
->setHeaderTitleFormat(self::TABLE_TITLE_STYLE);
$tableRows = $this->withRowSeparators ? intersperse($rows, new TableSeparator()) : $rows;
$table = clone $this->baseTable;
$table->setStyle($style)
->setHeaders($headers)
->setRows($rows)
->setRows($tableRows)
->setFooterTitle($footerTitle)
->setHeaderTitle($headerTitle)
->render();