123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Console\Style;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Exception\RuntimeException;
- use Symfony\Component\Console\Formatter\OutputFormatter;
- use Symfony\Component\Console\Helper\Helper;
- use Symfony\Component\Console\Helper\OutputWrapper;
- use Symfony\Component\Console\Helper\ProgressBar;
- use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
- use Symfony\Component\Console\Helper\Table;
- 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\ConsoleSectionOutput;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Output\TrimmedBufferOutput;
- use Symfony\Component\Console\Question\ChoiceQuestion;
- use Symfony\Component\Console\Question\ConfirmationQuestion;
- use Symfony\Component\Console\Question\Question;
- use Symfony\Component\Console\Terminal;
- /**
- * Output decorator helpers for the Symfony Style Guide.
- *
- * @author Kevin Bond <kevinbond@gmail.com>
- */
- class SymfonyStyle extends OutputStyle
- {
- public const MAX_LINE_LENGTH = 120;
- private InputInterface $input;
- private OutputInterface $output;
- private SymfonyQuestionHelper $questionHelper;
- private ProgressBar $progressBar;
- private int $lineLength;
- private TrimmedBufferOutput $bufferedOutput;
- public function __construct(InputInterface $input, OutputInterface $output)
- {
- $this->input = $input;
- $this->bufferedOutput = new TrimmedBufferOutput(\DIRECTORY_SEPARATOR === '\\' ? 4 : 2, $output->getVerbosity(), false, clone $output->getFormatter());
- // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
- $width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
- $this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
- parent::__construct($this->output = $output);
- }
- /**
- * Formats a message as a block of text.
- *
- * @return void
- */
- public function block(string|array $messages, ?string $type = null, ?string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = true)
- {
- $messages = \is_array($messages) ? array_values($messages) : [$messages];
- $this->autoPrependBlock();
- $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, $escape));
- $this->newLine();
- }
- /**
- * @return void
- */
- public function title(string $message)
- {
- $this->autoPrependBlock();
- $this->writeln([
- sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
- sprintf('<comment>%s</>', str_repeat('=', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))),
- ]);
- $this->newLine();
- }
- /**
- * @return void
- */
- public function section(string $message)
- {
- $this->autoPrependBlock();
- $this->writeln([
- sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
- sprintf('<comment>%s</>', str_repeat('-', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))),
- ]);
- $this->newLine();
- }
- /**
- * @return void
- */
- public function listing(array $elements)
- {
- $this->autoPrependText();
- $elements = array_map(fn ($element) => sprintf(' * %s', $element), $elements);
- $this->writeln($elements);
- $this->newLine();
- }
- /**
- * @return void
- */
- public function text(string|array $message)
- {
- $this->autoPrependText();
- $messages = \is_array($message) ? array_values($message) : [$message];
- foreach ($messages as $message) {
- $this->writeln(sprintf(' %s', $message));
- }
- }
- /**
- * Formats a command comment.
- *
- * @return void
- */
- public function comment(string|array $message)
- {
- $this->block($message, null, null, '<fg=default;bg=default> // </>', false, false);
- }
- /**
- * @return void
- */
- public function success(string|array $message)
- {
- $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
- }
- /**
- * @return void
- */
- public function error(string|array $message)
- {
- $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
- }
- /**
- * @return void
- */
- public function warning(string|array $message)
- {
- $this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ', true);
- }
- /**
- * @return void
- */
- public function note(string|array $message)
- {
- $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
- }
- /**
- * Formats an info message.
- *
- * @return void
- */
- public function info(string|array $message)
- {
- $this->block($message, 'INFO', 'fg=green', ' ', true);
- }
- /**
- * @return void
- */
- public function caution(string|array $message)
- {
- $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
- }
- /**
- * @return void
- */
- public function table(array $headers, array $rows)
- {
- $this->createTable()
- ->setHeaders($headers)
- ->setRows($rows)
- ->render()
- ;
- $this->newLine();
- }
- /**
- * Formats a horizontal table.
- *
- * @return void
- */
- public function horizontalTable(array $headers, array $rows)
- {
- $this->createTable()
- ->setHorizontal(true)
- ->setHeaders($headers)
- ->setRows($rows)
- ->render()
- ;
- $this->newLine();
- }
- /**
- * Formats a list of key/value horizontally.
- *
- * Each row can be one of:
- * * 'A title'
- * * ['key' => 'value']
- * * new TableSeparator()
- *
- * @return void
- */
- public function definitionList(string|array|TableSeparator ...$list)
- {
- $headers = [];
- $row = [];
- foreach ($list as $value) {
- if ($value instanceof TableSeparator) {
- $headers[] = $value;
- $row[] = $value;
- continue;
- }
- if (\is_string($value)) {
- $headers[] = new TableCell($value, ['colspan' => 2]);
- $row[] = null;
- continue;
- }
- if (!\is_array($value)) {
- throw new InvalidArgumentException('Value should be an array, string, or an instance of TableSeparator.');
- }
- $headers[] = key($value);
- $row[] = current($value);
- }
- $this->horizontalTable($headers, [$row]);
- }
- public function ask(string $question, ?string $default = null, ?callable $validator = null): mixed
- {
- $question = new Question($question, $default);
- $question->setValidator($validator);
- return $this->askQuestion($question);
- }
- public function askHidden(string $question, ?callable $validator = null): mixed
- {
- $question = new Question($question);
- $question->setHidden(true);
- $question->setValidator($validator);
- return $this->askQuestion($question);
- }
- public function confirm(string $question, bool $default = true): bool
- {
- return $this->askQuestion(new ConfirmationQuestion($question, $default));
- }
- public function choice(string $question, array $choices, mixed $default = null, bool $multiSelect = false): mixed
- {
- if (null !== $default) {
- $values = array_flip($choices);
- $default = $values[$default] ?? $default;
- }
- $questionChoice = new ChoiceQuestion($question, $choices, $default);
- $questionChoice->setMultiselect($multiSelect);
- return $this->askQuestion($questionChoice);
- }
- /**
- * @return void
- */
- public function progressStart(int $max = 0)
- {
- $this->progressBar = $this->createProgressBar($max);
- $this->progressBar->start();
- }
- /**
- * @return void
- */
- public function progressAdvance(int $step = 1)
- {
- $this->getProgressBar()->advance($step);
- }
- /**
- * @return void
- */
- public function progressFinish()
- {
- $this->getProgressBar()->finish();
- $this->newLine(2);
- unset($this->progressBar);
- }
- public function createProgressBar(int $max = 0): ProgressBar
- {
- $progressBar = parent::createProgressBar($max);
- if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
- $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
- $progressBar->setProgressCharacter('');
- $progressBar->setBarCharacter('▓'); // dark shade character \u2593
- }
- return $progressBar;
- }
- /**
- * @see ProgressBar::iterate()
- *
- * @template TKey
- * @template TValue
- *
- * @param iterable<TKey, TValue> $iterable
- * @param int|null $max Number of steps to complete the bar (0 if indeterminate), if null it will be inferred from $iterable
- *
- * @return iterable<TKey, TValue>
- */
- public function progressIterate(iterable $iterable, ?int $max = null): iterable
- {
- yield from $this->createProgressBar()->iterate($iterable, $max);
- $this->newLine(2);
- }
- public function askQuestion(Question $question): mixed
- {
- if ($this->input->isInteractive()) {
- $this->autoPrependBlock();
- }
- $this->questionHelper ??= new SymfonyQuestionHelper();
- $answer = $this->questionHelper->ask($this->input, $this, $question);
- if ($this->input->isInteractive()) {
- if ($this->output instanceof ConsoleSectionOutput) {
- // add the new line of the `return` to submit the input to ConsoleSectionOutput, because ConsoleSectionOutput is holding all it's lines.
- // this is relevant when a `ConsoleSectionOutput::clear` is called.
- $this->output->addNewLineOfInputSubmit();
- }
- $this->newLine();
- $this->bufferedOutput->write("\n");
- }
- return $answer;
- }
- /**
- * @return void
- */
- public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL)
- {
- if (!is_iterable($messages)) {
- $messages = [$messages];
- }
- foreach ($messages as $message) {
- parent::writeln($message, $type);
- $this->writeBuffer($message, true, $type);
- }
- }
- /**
- * @return void
- */
- public function write(string|iterable $messages, bool $newline = false, int $type = self::OUTPUT_NORMAL)
- {
- if (!is_iterable($messages)) {
- $messages = [$messages];
- }
- foreach ($messages as $message) {
- parent::write($message, $newline, $type);
- $this->writeBuffer($message, $newline, $type);
- }
- }
- /**
- * @return void
- */
- public function newLine(int $count = 1)
- {
- parent::newLine($count);
- $this->bufferedOutput->write(str_repeat("\n", $count));
- }
- /**
- * Returns a new instance which makes use of stderr if available.
- */
- public function getErrorStyle(): self
- {
- 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
- {
- return $this->progressBar
- ?? throw new RuntimeException('The ProgressBar is not started.');
- }
- private function autoPrependBlock(): void
- {
- $chars = substr(str_replace(\PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
- if (!isset($chars[0])) {
- $this->newLine(); // empty history, so we should start with a new line.
- return;
- }
- // Prepend new line for each non LF chars (This means no blank line was output before)
- $this->newLine(2 - substr_count($chars, "\n"));
- }
- private function autoPrependText(): void
- {
- $fetched = $this->bufferedOutput->fetch();
- // Prepend new line if last char isn't EOL:
- if ($fetched && !str_ends_with($fetched, "\n")) {
- $this->newLine();
- }
- }
- private function writeBuffer(string $message, bool $newLine, int $type): void
- {
- // We need to know if the last chars are PHP_EOL
- $this->bufferedOutput->write($message, $newLine, $type);
- }
- private function createBlock(iterable $messages, ?string $type = null, ?string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array
- {
- $indentLength = 0;
- $prefixLength = Helper::width(Helper::removeDecoration($this->getFormatter(), $prefix));
- $lines = [];
- if (null !== $type) {
- $type = sprintf('[%s] ', $type);
- $indentLength = Helper::width($type);
- $lineIndentation = str_repeat(' ', $indentLength);
- }
- // wrap and add newlines for each element
- $outputWrapper = new OutputWrapper();
- foreach ($messages as $key => $message) {
- if ($escape) {
- $message = OutputFormatter::escape($message);
- }
- $lines = array_merge(
- $lines,
- explode(\PHP_EOL, $outputWrapper->wrap(
- $message,
- $this->lineLength - $prefixLength - $indentLength,
- \PHP_EOL
- ))
- );
- if (\count($messages) > 1 && $key < \count($messages) - 1) {
- $lines[] = '';
- }
- }
- $firstLineIndex = 0;
- if ($padding && $this->isDecorated()) {
- $firstLineIndex = 1;
- array_unshift($lines, '');
- $lines[] = '';
- }
- foreach ($lines as $i => &$line) {
- if (null !== $type) {
- $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
- }
- $line = $prefix.$line;
- $line .= str_repeat(' ', max($this->lineLength - Helper::width(Helper::removeDecoration($this->getFormatter(), $line)), 0));
- if ($style) {
- $line = sprintf('<%s>%s</>', $style, $line);
- }
- }
- return $lines;
- }
- }
|