annotate vendor/symfony/console/Style/SymfonyStyle.php @ 0:4c8ae668cc8c

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