8000 [Console] SymfonyStyle table improvements/customizations: · symfony/symfony@7218851 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7218851

Browse files
committed
[Console] SymfonyStyle table improvements/customizations:
- allow configuring table style - add createTable method - use wrapped output to allow creation of "appendable" tables
1 parent 1562c98 commit 7218851

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SymfonyStyle extends OutputStyle
3838
public const MAX_LINE_LENGTH = 120;
3939

4040
private $input;
41+
private $output;
4142
private $questionHelper;
4243
private $progressBar;
4344
private $lineLength;
@@ -51,7 +52,7 @@ public function __construct(InputInterface $input, OutputInterface $output)
5152
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
5253
$this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
5354

54-
parent::__construct($output);
55+
parent::__construct($this->output = $output);
5556
}
5657

5758
/**
@@ -186,15 +187,12 @@ public function caution($message)
186187
*/
187188
public function table(array $headers, array $rows)
188189
{
189-
$style = clone Table::getStyleDefinition('symfony-style-guide');
190-
$style->setCellHeaderFormat('<info>%s</info>');
191-
192-
$table = new Table($this);
193-
$table->setHeaders($headers);
194-
$table->setRows($rows);
195-
$table->setStyle($style);
190+
$this->createTable()
191+
->setHeaders($headers)
192+
->setRows($rows)
193+
->render()
194+
;
196195

197-
$table->render();
198196
$this->newLine();
199197
}
200198

@@ -203,16 +201,13 @@ public function table(array $headers, array $rows)
203201
*/
204202
public function horizontalTable(array $headers, array $rows)
205203
{
206-
$style = clone Table::getStyleDefinition('symfony-style-guide');
207-
$style->setCellHeaderFormat('<info>%s</info>');
204+
$this->createTable()
205+
->setHorizontal(true)
206+
->setHeaders($headers)
207+
->setRows($rows)
208+
->render()
209+
;
208210

209-
$table = new Table($this);
210-
$table->setHeaders($headers);
211-
$table->setRows($rows);
212-
$table->setStyle($style);
213-
$table->setHorizontal(true);
214-
215-
$table->render();
216211
$this->newLine();
217212
}
218213

@@ -228,10 +223,6 @@ public function horizontalTable(array $headers, array $rows)
228223
*/
229224
public function definitionList(...$list)
230225
{
231-
$style = clone Table::getStyleDefinition('symfony-style-guide');
232-
$style->setCellHeaderFormat('<info>%s</info>');
233-
234-
$table = new Table($this);
235226
$headers = [];
236227
$row = [];
237228
foreach ($list as $value) {
@@ -252,13 +243,7 @@ public function definitionList(...$list)
252243
$row[] = current($value);
253244
}
254245

255-
$table->setHeaders($headers);
256-
$table->setRows([$row]);
257-
$table->setHorizontal();
258-
$table->setStyle($style);
259-
260-
$table->render();
261-
$this->newLine();
246+
$this->horizontalTable($headers, [$row]);
262247
}
263248

264249
/**
@@ -421,6 +406,14 @@ public function getErrorStyle()
421406
return new self($this->input, $this->getErrorOutput());
422407
}
423408

409+
public function createTable(): Table
410+
{
411+
$style = clone Table::getStyleDefinition('symfony-style-guide');
412+
$style->setCellHeaderFormat('<info>%s</info>');
413+
414+
return (new Table($this->output))->setStyle($style);
415+
}
416+
424417
private function getProgressBar(): ProgressBar
425418
{
426419
if (!$this->progressBar) {

0 commit comments

Comments
 (0)
0