Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] SymfonyStyle enhancements #43595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console] SymfonyStyle enhancements
  • Loading branch information
kbond authored and fabpot committed Oct 20, 2021
commit 6740ae55ee5922c1a63f1b6d102914ed8e5b9b77
63 changes: 34 additions & 29 deletions 63 src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\TrimmedBufferOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;
Expand All @@ -38,6 +39,7 @@ class SymfonyStyle extends OutputStyle
public const MAX_LINE_LENGTH = 120;

private $input;
private $output;
private $questionHelper;
private $progressBar;
private $lineLength;
Expand All @@ -51,7 +53,7 @@ public function __construct(InputInterface $input, OutputInterface $output)
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
$this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);

parent::__construct($output);
parent::__construct($this->output = $output);
}

/**
Expand Down Expand Up @@ -186,15 +188,12 @@ public function caution($message)
*/
public function table(array $headers, array $rows)
{
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');

$table = new Table($this);
$table->setHeaders($headers);
$table->setRows($rows);
$table->setStyle($style);
$this->createTable()
->setHeaders($headers)
->setRows($rows)
->render()
;

$table->render();
$this->newLine();
}

Expand All @@ -203,16 +202,13 @@ public function table(array $headers, array $rows)
*/
public function horizontalTable(array $headers, array $rows)
{
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');

$table = new Table($this);
$table->setHeaders($headers);
$table->setRows($rows);
$table->setStyle($style);
$table->setHorizontal(true);
$this->createTable()
->setHorizontal(true)
->setHeaders($headers)
->setRows($rows)
->render()
;

$table->render();
$this->newLine();
}

Expand All @@ -228,10 +224,6 @@ public function horizontalTable(array $headers, array $rows)
*/
public function definitionList(...$list)
{
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');

$table = new Table($this);
$headers = [];
$row = [];
foreach ($list as $value) {
Expand All @@ -252,13 +244,7 @@ public function definitionList(...$list)
$row[] = current($value);
}

$table->setHeaders($headers);
$table->setRows([$row]);
$table->setHorizontal();
$table->setStyle($style);

$table->render();
$this->newLine();
$this->horizontalTable($headers, [$row]);
}

/**
Expand Down Expand Up @@ -349,6 +335,16 @@ public function createProgressBar(int $max = 0)
return $progressBar;
}

/**
* @see ProgressBar::iterate()
*/
public function progressIterate(iterable $iterable, int $max = null): iterable
{
yield from $this->createProgressBar()->iterate($iterable, $max);

$this->newLine(2);
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -421,6 +417,15 @@ public function getErrorStyle()
return new self($this->input, $this->getErrorOutput());
}

public function createTable(): Table
{
$output = $this->output instanceof ConsoleOutputInterface ? $this->output->section() : $this->output;
$style = clone Table::getStyleDefinition('symfony-style-guide');
$style->setCellHeaderFormat('<info>%s</info>');

return (new Table($output))->setStyle($style);
}

private function getProgressBar(): ProgressBar
{
if (!$this->progressBar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

// progressIterate
return function (InputInterface $input, OutputInterface $output) {
$style = new SymfonyStyle($input, $output);

foreach ($style->progressIterate(\range(1, 10)) as $step) {
// noop
}

$style->writeln('end of progressbar');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0/10 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
10/10 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

end of progressbar
35 changes: 35 additions & 0 deletions 35 src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -106,6 +108,39 @@ public function testGetErrorStyle()
$io->getErrorStyle()->write('');
}

public function testCreateTableWithConsoleOutput()
{
$input = $this->createMock(InputInterface::class);
$output = $this->createMock(ConsoleOutputInterface::class);
$output
->method('getFormatter')
->willReturn(new OutputFormatter());
$output
->expects($this->once())
->method('section')
->willReturn($this->createMock(ConsoleSectionOutput::class));

$style = new SymfonyStyle($input, $output);

$style->createTable();
}

public function testCreateTableWithoutConsoleOutput()
{
$input = $this->createMock(InputInterface::class);
$output = $this->createMock(OutputInterface::class);
$output
->method('getFormatter')
->willReturn(new OutputFormatter());

$style = new SymfonyStyle($input, $output);

$this->expectException(RuntimeException::class);
$this->expectDeprecationMessage('Output should be an instance of "Symfony\Component\Console\Output\ConsoleSectionOutput"');

$style->createTable()->appendRow(['row']);
}

public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable()
{
$output = $this->createMock(OutputInterface::class);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.