annotate vendor/symfony/console/Style/SymfonyStyle.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Style;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\RuntimeException;
Chris@0 15 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@0 16 use Symfony\Component\Console\Helper\Helper;
Chris@0 17 use Symfony\Component\Console\Helper\ProgressBar;
Chris@0 18 use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
Chris@0 19 use Symfony\Component\Console\Helper\Table;
Chris@0 20 use Symfony\Component\Console\Input\InputInterface;
Chris@0 21 use Symfony\Component\Console\Output\BufferedOutput;
Chris@0 22 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 23 use Symfony\Component\Console\Question\ChoiceQuestion;
Chris@0 24 use Symfony\Component\Console\Question\ConfirmationQuestion;
Chris@0 25 use Symfony\Component\Console\Question\Question;
Chris@0 26 use Symfony\Component\Console\Terminal;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Output decorator helpers for the Symfony Style Guide.
Chris@0 30 *
Chris@0 31 * @author Kevin Bond <kevinbond@gmail.com>
Chris@0 32 */
Chris@0 33 class SymfonyStyle extends OutputStyle
Chris@0 34 {
Chris@0 35 const MAX_LINE_LENGTH = 120;
Chris@0 36
Chris@0 37 private $input;
Chris@0 38 private $questionHelper;
Chris@0 39 private $progressBar;
Chris@0 40 private $lineLength;
Chris@0 41 private $bufferedOutput;
Chris@0 42
Chris@0 43 public function __construct(InputInterface $input, OutputInterface $output)
Chris@0 44 {
Chris@0 45 $this->input = $input;
Chris@0 46 $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
Chris@0 47 // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
Chris@0 48 $width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
Chris@17 49 $this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
Chris@0 50
Chris@0 51 parent::__construct($output);
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Formats a message as a block of text.
Chris@0 56 *
Chris@0 57 * @param string|array $messages The message to write in the block
Chris@0 58 * @param string|null $type The block type (added in [] on first line)
Chris@0 59 * @param string|null $style The style to apply to the whole block
Chris@0 60 * @param string $prefix The prefix for the block
Chris@0 61 * @param bool $padding Whether to add vertical padding
Chris@14 62 * @param bool $escape Whether to escape the message
Chris@0 63 */
Chris@14 64 public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = true)
Chris@0 65 {
Chris@17 66 $messages = \is_array($messages) ? array_values($messages) : [$messages];
Chris@0 67
Chris@0 68 $this->autoPrependBlock();
Chris@14 69 $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, $escape));
Chris@0 70 $this->newLine();
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * {@inheritdoc}
Chris@0 75 */
Chris@0 76 public function title($message)
Chris@0 77 {
Chris@0 78 $this->autoPrependBlock();
Chris@17 79 $this->writeln([
Chris@0 80 sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
Chris@0 81 sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
Chris@17 82 ]);
Chris@0 83 $this->newLine();
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function section($message)
Chris@0 90 {
Chris@0 91 $this->autoPrependBlock();
Chris@17 92 $this->writeln([
Chris@0 93 sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
Chris@0 94 sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
Chris@17 95 ]);
Chris@0 96 $this->newLine();
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * {@inheritdoc}
Chris@0 101 */
Chris@0 102 public function listing(array $elements)
Chris@0 103 {
Chris@0 104 $this->autoPrependText();
Chris@0 105 $elements = array_map(function ($element) {
Chris@0 106 return sprintf(' * %s', $element);
Chris@0 107 }, $elements);
Chris@0 108
Chris@0 109 $this->writeln($elements);
Chris@0 110 $this->newLine();
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * {@inheritdoc}
Chris@0 115 */
Chris@0 116 public function text($message)
Chris@0 117 {
Chris@0 118 $this->autoPrependText();
Chris@0 119
Chris@17 120 $messages = \is_array($message) ? array_values($message) : [$message];
Chris@0 121 foreach ($messages as $message) {
Chris@0 122 $this->writeln(sprintf(' %s', $message));
Chris@0 123 }
Chris@0 124 }
Chris@0 125
Chris@0 126 /**
Chris@0 127 * Formats a command comment.
Chris@0 128 *
Chris@0 129 * @param string|array $message
Chris@0 130 */
Chris@0 131 public function comment($message)
Chris@0 132 {
Chris@14 133 $this->block($message, null, null, '<fg=default;bg=default> // </>', false, false);
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * {@inheritdoc}
Chris@0 138 */
Chris@0 139 public function success($message)
Chris@0 140 {
Chris@0 141 $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * {@inheritdoc}
Chris@0 146 */
Chris@0 147 public function error($message)
Chris@0 148 {
Chris@0 149 $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * {@inheritdoc}
Chris@0 154 */
Chris@0 155 public function warning($message)
Chris@0 156 {
Chris@0 157 $this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true);
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function note($message)
Chris@0 164 {
Chris@0 165 $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * {@inheritdoc}
Chris@0 170 */
Chris@0 171 public function caution($message)
Chris@0 172 {
Chris@0 173 $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * {@inheritdoc}
Chris@0 178 */
Chris@0 179 public function table(array $headers, array $rows)
Chris@0 180 {
Chris@0 181 $style = clone Table::getStyleDefinition('symfony-style-guide');
Chris@0 182 $style->setCellHeaderFormat('<info>%s</info>');
Chris@0 183
Chris@0 184 $table = new Table($this);
Chris@0 185 $table->setHeaders($headers);
Chris@0 186 $table->setRows($rows);
Chris@0 187 $table->setStyle($style);
Chris@0 188
Chris@0 189 $table->render();
Chris@0 190 $this->newLine();
Chris@0 191 }
Chris@0 192
Chris@0 193 /**
Chris@0 194 * {@inheritdoc}
Chris@0 195 */
Chris@0 196 public function ask($question, $default = null, $validator = null)
Chris@0 197 {
Chris@0 198 $question = new Question($question, $default);
Chris@0 199 $question->setValidator($validator);
Chris@0 200
Chris@0 201 return $this->askQuestion($question);
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * {@inheritdoc}
Chris@0 206 */
Chris@0 207 public function askHidden($question, $validator = null)
Chris@0 208 {
Chris@0 209 $question = new Question($question);
Chris@0 210
Chris@0 211 $question->setHidden(true);
Chris@0 212 $question->setValidator($validator);
Chris@0 213
Chris@0 214 return $this->askQuestion($question);
Chris@0 215 }
Chris@0 216
Chris@0 217 /**
Chris@0 218 * {@inheritdoc}
Chris@0 219 */
Chris@0 220 public function confirm($question, $default = true)
Chris@0 221 {
Chris@0 222 return $this->askQuestion(new ConfirmationQuestion($question, $default));
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * {@inheritdoc}
Chris@0 227 */
Chris@0 228 public function choice($question, array $choices, $default = null)
Chris@0 229 {
Chris@0 230 if (null !== $default) {
Chris@0 231 $values = array_flip($choices);
Chris@0 232 $default = $values[$default];
Chris@0 233 }
Chris@0 234
Chris@0 235 return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * {@inheritdoc}
Chris@0 240 */
Chris@0 241 public function progressStart($max = 0)
Chris@0 242 {
Chris@0 243 $this->progressBar = $this->createProgressBar($max);
Chris@0 244 $this->progressBar->start();
Chris@0 245 }
Chris@0 246
Chris@0 247 /**
Chris@0 248 * {@inheritdoc}
Chris@0 249 */
Chris@0 250 public function progressAdvance($step = 1)
Chris@0 251 {
Chris@0 252 $this->getProgressBar()->advance($step);
Chris@0 253 }
Chris@0 254
Chris@0 255 /**
Chris@0 256 * {@inheritdoc}
Chris@0 257 */
Chris@0 258 public function progressFinish()
Chris@0 259 {
Chris@0 260 $this->getProgressBar()->finish();
Chris@0 261 $this->newLine(2);
Chris@0 262 $this->progressBar = null;
Chris@0 263 }
Chris@0 264
Chris@0 265 /**
Chris@0 266 * {@inheritdoc}
Chris@0 267 */
Chris@0 268 public function createProgressBar($max = 0)
Chris@0 269 {
Chris@0 270 $progressBar = parent::createProgressBar($max);
Chris@0 271
Chris@17 272 if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
Chris@0 273 $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
Chris@0 274 $progressBar->setProgressCharacter('');
Chris@0 275 $progressBar->setBarCharacter('▓'); // dark shade character \u2593
Chris@0 276 }
Chris@0 277
Chris@0 278 return $progressBar;
Chris@0 279 }
Chris@0 280
Chris@0 281 /**
Chris@14 282 * @return mixed
Chris@0 283 */
Chris@0 284 public function askQuestion(Question $question)
Chris@0 285 {
Chris@0 286 if ($this->input->isInteractive()) {
Chris@0 287 $this->autoPrependBlock();
Chris@0 288 }
Chris@0 289
Chris@0 290 if (!$this->questionHelper) {
Chris@0 291 $this->questionHelper = new SymfonyQuestionHelper();
Chris@0 292 }
Chris@0 293
Chris@0 294 $answer = $this->questionHelper->ask($this->input, $this, $question);
Chris@0 295
Chris@0 296 if ($this->input->isInteractive()) {
Chris@0 297 $this->newLine();
Chris@0 298 $this->bufferedOutput->write("\n");
Chris@0 299 }
Chris@0 300
Chris@0 301 return $answer;
Chris@0 302 }
Chris@0 303
Chris@0 304 /**
Chris@0 305 * {@inheritdoc}
Chris@0 306 */
Chris@0 307 public function writeln($messages, $type = self::OUTPUT_NORMAL)
Chris@0 308 {
Chris@0 309 parent::writeln($messages, $type);
Chris@0 310 $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
Chris@0 311 }
Chris@0 312
Chris@0 313 /**
Chris@0 314 * {@inheritdoc}
Chris@0 315 */
Chris@0 316 public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
Chris@0 317 {
Chris@0 318 parent::write($messages, $newline, $type);
Chris@0 319 $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
Chris@0 320 }
Chris@0 321
Chris@0 322 /**
Chris@0 323 * {@inheritdoc}
Chris@0 324 */
Chris@0 325 public function newLine($count = 1)
Chris@0 326 {
Chris@0 327 parent::newLine($count);
Chris@0 328 $this->bufferedOutput->write(str_repeat("\n", $count));
Chris@0 329 }
Chris@0 330
Chris@0 331 /**
Chris@14 332 * Returns a new instance which makes use of stderr if available.
Chris@14 333 *
Chris@14 334 * @return self
Chris@14 335 */
Chris@14 336 public function getErrorStyle()
Chris@14 337 {
Chris@14 338 return new self($this->input, $this->getErrorOutput());
Chris@14 339 }
Chris@14 340
Chris@14 341 /**
Chris@0 342 * @return ProgressBar
Chris@0 343 */
Chris@0 344 private function getProgressBar()
Chris@0 345 {
Chris@0 346 if (!$this->progressBar) {
Chris@0 347 throw new RuntimeException('The ProgressBar is not started.');
Chris@0 348 }
Chris@0 349
Chris@0 350 return $this->progressBar;
Chris@0 351 }
Chris@0 352
Chris@0 353 private function autoPrependBlock()
Chris@0 354 {
Chris@0 355 $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
Chris@0 356
Chris@0 357 if (!isset($chars[0])) {
Chris@0 358 return $this->newLine(); //empty history, so we should start with a new line.
Chris@0 359 }
Chris@0 360 //Prepend new line for each non LF chars (This means no blank line was output before)
Chris@0 361 $this->newLine(2 - substr_count($chars, "\n"));
Chris@0 362 }
Chris@0 363
Chris@0 364 private function autoPrependText()
Chris@0 365 {
Chris@0 366 $fetched = $this->bufferedOutput->fetch();
Chris@0 367 //Prepend new line if last char isn't EOL:
Chris@0 368 if ("\n" !== substr($fetched, -1)) {
Chris@0 369 $this->newLine();
Chris@0 370 }
Chris@0 371 }
Chris@0 372
Chris@0 373 private function reduceBuffer($messages)
Chris@0 374 {
Chris@0 375 // We need to know if the two last chars are PHP_EOL
Chris@0 376 // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
Chris@0 377 return array_map(function ($value) {
Chris@0 378 return substr($value, -4);
Chris@17 379 }, array_merge([$this->bufferedOutput->fetch()], (array) $messages));
Chris@0 380 }
Chris@0 381
Chris@0 382 private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
Chris@0 383 {
Chris@0 384 $indentLength = 0;
Chris@0 385 $prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
Chris@17 386 $lines = [];
Chris@0 387
Chris@0 388 if (null !== $type) {
Chris@0 389 $type = sprintf('[%s] ', $type);
Chris@17 390 $indentLength = \strlen($type);
Chris@0 391 $lineIndentation = str_repeat(' ', $indentLength);
Chris@0 392 }
Chris@0 393
Chris@0 394 // wrap and add newlines for each element
Chris@0 395 foreach ($messages as $key => $message) {
Chris@0 396 if ($escape) {
Chris@0 397 $message = OutputFormatter::escape($message);
Chris@0 398 }
Chris@0 399
Chris@0 400 $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
Chris@0 401
Chris@17 402 if (\count($messages) > 1 && $key < \count($messages) - 1) {
Chris@0 403 $lines[] = '';
Chris@0 404 }
Chris@0 405 }
Chris@0 406
Chris@0 407 $firstLineIndex = 0;
Chris@0 408 if ($padding && $this->isDecorated()) {
Chris@0 409 $firstLineIndex = 1;
Chris@0 410 array_unshift($lines, '');
Chris@0 411 $lines[] = '';
Chris@0 412 }
Chris@0 413
Chris@0 414 foreach ($lines as $i => &$line) {
Chris@0 415 if (null !== $type) {
Chris@0 416 $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
Chris@0 417 }
Chris@0 418
Chris@0 419 $line = $prefix.$line;
Chris@0 420 $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
Chris@0 421
Chris@0 422 if ($style) {
Chris@0 423 $line = sprintf('<%s>%s</>', $style, $line);
Chris@0 424 }
Chris@0 425 }
Chris@0 426
Chris@0 427 return $lines;
Chris@0 428 }
Chris@0 429 }