* * 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 */ class SymfonyStyle extends OutputStyle { public const MAX_LINE_LENGTH = 120; /** * @var \Symfony\Component\Console\Input\InputInterface */ private $input; /** * @var \Symfony\Component\Console\Output\OutputInterface */ private $output; /** * @var \Symfony\Component\Console\Helper\SymfonyQuestionHelper */ private $questionHelper; /** * @var \Symfony\Component\Console\Helper\ProgressBar */ private $progressBar; /** * @var int */ private $lineLength; /** * @var \Symfony\Component\Console\Output\TrimmedBufferOutput */ private $bufferedOutput; /** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output */ public function __construct($input, $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 * @param string|mixed[] $messages * @param string|null $type * @param string|null $style * @param string $prefix * @param bool $padding * @param bool $escape */ public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $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 * @param string $message */ public function title($message) { $this->autoPrependBlock(); $this->writeln([ sprintf('%s', OutputFormatter::escapeTrailingBackslash($message)), sprintf('%s', str_repeat('=', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))), ]); $this->newLine(); } /** * @return void * @param string $message */ public function section($message) { $this->autoPrependBlock(); $this->writeln([ sprintf('%s', OutputFormatter::escapeTrailingBackslash($message)), sprintf('%s', str_repeat('-', Helper::width(Helper::removeDecoration($this->getFormatter(), $message)))), ]); $this->newLine(); } /** * @return void * @param mixed[] $elements */ public function listing($elements) { $this->autoPrependText(); $elements = array_map(function ($element) { return sprintf(' * %s', $element); }, $elements); $this->writeln($elements); $this->newLine(); } /** * @return void * @param string|mixed[] $message */ public function text($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 * @param string|mixed[] $message */ public function comment($message) { $this->block($message, null, null, ' // ', false, false); } /** * @return void * @param string|mixed[] $message */ public function success($message) { $this->block($message, 'OK', 'fg=black;bg=green', ' ', true); } /** * @return void * @param string|mixed[] $message */ public function error($message) { $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true); } /** * @return void * @param string|mixed[] $message */ public function warning($message) { $this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ', true); } /** * @return void * @param string|mixed[] $message */ public function note($message) { $this->block($message, 'NOTE', 'fg=yellow', ' ! '); } /** * Formats an info message. * * @return void * @param string|mixed[] $message */ public function info($message) { $this->block($message, 'INFO', 'fg=green', ' ', true); } /** * @return void * @param string|mixed[] $message */ public function caution($message) { $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true); } /** * @return void * @param mixed[] $headers * @param mixed[] $rows */ public function table($headers, $rows) { $this->createTable() ->setHeaders($headers) ->setRows($rows) ->render() ; $this->newLine(); } /** * Formats a horizontal table. * * @return void * @param mixed[] $headers * @param mixed[] $rows */ public function horizontalTable($headers, $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 * @param string|mixed[]|\Symfony\Component\Console\Helper\TableSeparator ...$list */ public function definitionList(...$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]); } /** * @return mixed * @param string $question * @param string|null $default * @param callable|null $validator */ public function ask($question, $default = null, $validator = null) { $question = new Question($question, $default); $question->setValidator($validator); return $this->askQuestion($question); } /** * @return mixed * @param string $question * @param callable|null $validator */ public function askHidden($question, $validator = null) { $question = new Question($question); $question->setHidden(true); $question->setValidator($validator); return $this->askQuestion($question); } /** * @param string $question * @param bool $default */ public function confirm($question, $default = true) { return $this->askQuestion(new ConfirmationQuestion($question, $default)); } /** * @param mixed $default * @return mixed * @param string $question * @param mixed[] $choices * @param bool $multiSelect */ public function choice($question, $choices, $default = null, $multiSelect = false) { 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 * @param int $max */ public function progressStart($max = 0) { $this->progressBar = $this->createProgressBar($max); $this->progressBar->start(); } /** * @return void * @param int $step */ public function progressAdvance($step = 1) { $this->getProgressBar()->advance($step); } /** * @return void */ public function progressFinish() { $this->getProgressBar()->finish(); $this->newLine(2); unset($this->progressBar); } /** * @param int $max */ public function createProgressBar($max = 0) { $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() * @param mixed[] $iterable * @param int|null $max */ public function progressIterate($iterable, $max = null) { yield from $this->createProgressBar()->iterate($iterable, $max); $this->newLine(2); } /** * @return mixed * @param \Symfony\Component\Console\Question\Question $question */ public function askQuestion($question) { if ($this->input->isInteractive()) { $this->autoPrependBlock(); } $this->questionHelper = $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 * @param string|mixed[] $messages * @param int $type */ public function writeln($messages, $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 * @param string|mixed[] $messages * @param bool $newline * @param int $type */ public function write($messages, $newline = false, $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 * @param int $count */ public function newLine($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() { return new self($this->input, $this->getErrorOutput()); } public function createTable() { $output = $this->output instanceof ConsoleOutputInterface ? $this->output->section() : $this->output; $style = clone Table::getStyleDefinition('symfony-style-guide'); $style->setCellHeaderFormat('%s'); return (new Table($output))->setStyle($style); } private function getProgressBar() { if (!isset($this->progressBar)) { throw new RuntimeException('The ProgressBar is not started.'); } return $this->progressBar; } private function autoPrependBlock() { $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() { $fetched = $this->bufferedOutput->fetch(); // Prepend new line if last char isn't EOL: if ($fetched && substr_compare($fetched, "\n", -strlen("\n")) !== 0) { $this->newLine(); } } /** * @param string $message * @param bool $newLine * @param int $type */ private function writeBuffer($message, $newLine, $type) { // We need to know if the last chars are PHP_EOL $this->bufferedOutput->write($message, $newLine, $type); } /** * @param mixed[] $messages * @param string|null $type * @param string|null $style * @param string $prefix * @param bool $padding * @param bool $escape */ private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false) { $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; } }