annotate vendor/symfony/console/Helper/ProgressBar.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
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\Helper;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Output\ConsoleOutputInterface;
Chris@0 15 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 16 use Symfony\Component\Console\Exception\LogicException;
Chris@0 17 use Symfony\Component\Console\Terminal;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The ProgressBar provides helpers to display progress output.
Chris@0 21 *
Chris@0 22 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 23 * @author Chris Jones <leeked@gmail.com>
Chris@0 24 */
Chris@0 25 class ProgressBar
Chris@0 26 {
Chris@0 27 // options
Chris@0 28 private $barWidth = 28;
Chris@0 29 private $barChar;
Chris@0 30 private $emptyBarChar = '-';
Chris@0 31 private $progressChar = '>';
Chris@0 32 private $format;
Chris@0 33 private $internalFormat;
Chris@0 34 private $redrawFreq = 1;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @var OutputInterface
Chris@0 38 */
Chris@0 39 private $output;
Chris@0 40 private $step = 0;
Chris@0 41 private $max;
Chris@0 42 private $startTime;
Chris@0 43 private $stepWidth;
Chris@0 44 private $percent = 0.0;
Chris@0 45 private $formatLineCount;
Chris@0 46 private $messages = array();
Chris@0 47 private $overwrite = true;
Chris@0 48 private $terminal;
Chris@0 49 private $firstRun = true;
Chris@0 50
Chris@0 51 private static $formatters;
Chris@0 52 private static $formats;
Chris@0 53
Chris@0 54 /**
Chris@0 55 * @param OutputInterface $output An OutputInterface instance
Chris@0 56 * @param int $max Maximum steps (0 if unknown)
Chris@0 57 */
Chris@0 58 public function __construct(OutputInterface $output, $max = 0)
Chris@0 59 {
Chris@0 60 if ($output instanceof ConsoleOutputInterface) {
Chris@0 61 $output = $output->getErrorOutput();
Chris@0 62 }
Chris@0 63
Chris@0 64 $this->output = $output;
Chris@0 65 $this->setMaxSteps($max);
Chris@0 66 $this->terminal = new Terminal();
Chris@0 67
Chris@0 68 if (!$this->output->isDecorated()) {
Chris@0 69 // disable overwrite when output does not support ANSI codes.
Chris@0 70 $this->overwrite = false;
Chris@0 71
Chris@0 72 // set a reasonable redraw frequency so output isn't flooded
Chris@0 73 $this->setRedrawFrequency($max / 10);
Chris@0 74 }
Chris@0 75
Chris@0 76 $this->startTime = time();
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Sets a placeholder formatter for a given name.
Chris@0 81 *
Chris@0 82 * This method also allow you to override an existing placeholder.
Chris@0 83 *
Chris@0 84 * @param string $name The placeholder name (including the delimiter char like %)
Chris@0 85 * @param callable $callable A PHP callable
Chris@0 86 */
Chris@0 87 public static function setPlaceholderFormatterDefinition($name, callable $callable)
Chris@0 88 {
Chris@0 89 if (!self::$formatters) {
Chris@0 90 self::$formatters = self::initPlaceholderFormatters();
Chris@0 91 }
Chris@0 92
Chris@0 93 self::$formatters[$name] = $callable;
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Gets the placeholder formatter for a given name.
Chris@0 98 *
Chris@0 99 * @param string $name The placeholder name (including the delimiter char like %)
Chris@0 100 *
Chris@0 101 * @return callable|null A PHP callable
Chris@0 102 */
Chris@0 103 public static function getPlaceholderFormatterDefinition($name)
Chris@0 104 {
Chris@0 105 if (!self::$formatters) {
Chris@0 106 self::$formatters = self::initPlaceholderFormatters();
Chris@0 107 }
Chris@0 108
Chris@0 109 return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Sets a format for a given name.
Chris@0 114 *
Chris@0 115 * This method also allow you to override an existing format.
Chris@0 116 *
Chris@0 117 * @param string $name The format name
Chris@0 118 * @param string $format A format string
Chris@0 119 */
Chris@0 120 public static function setFormatDefinition($name, $format)
Chris@0 121 {
Chris@0 122 if (!self::$formats) {
Chris@0 123 self::$formats = self::initFormats();
Chris@0 124 }
Chris@0 125
Chris@0 126 self::$formats[$name] = $format;
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * Gets the format for a given name.
Chris@0 131 *
Chris@0 132 * @param string $name The format name
Chris@0 133 *
Chris@0 134 * @return string|null A format string
Chris@0 135 */
Chris@0 136 public static function getFormatDefinition($name)
Chris@0 137 {
Chris@0 138 if (!self::$formats) {
Chris@0 139 self::$formats = self::initFormats();
Chris@0 140 }
Chris@0 141
Chris@0 142 return isset(self::$formats[$name]) ? self::$formats[$name] : null;
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * Associates a text with a named placeholder.
Chris@0 147 *
Chris@0 148 * The text is displayed when the progress bar is rendered but only
Chris@0 149 * when the corresponding placeholder is part of the custom format line
Chris@0 150 * (by wrapping the name with %).
Chris@0 151 *
Chris@0 152 * @param string $message The text to associate with the placeholder
Chris@0 153 * @param string $name The name of the placeholder
Chris@0 154 */
Chris@0 155 public function setMessage($message, $name = 'message')
Chris@0 156 {
Chris@0 157 $this->messages[$name] = $message;
Chris@0 158 }
Chris@0 159
Chris@0 160 public function getMessage($name = 'message')
Chris@0 161 {
Chris@0 162 return $this->messages[$name];
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Gets the progress bar start time.
Chris@0 167 *
Chris@0 168 * @return int The progress bar start time
Chris@0 169 */
Chris@0 170 public function getStartTime()
Chris@0 171 {
Chris@0 172 return $this->startTime;
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * Gets the progress bar maximal steps.
Chris@0 177 *
Chris@0 178 * @return int The progress bar max steps
Chris@0 179 */
Chris@0 180 public function getMaxSteps()
Chris@0 181 {
Chris@0 182 return $this->max;
Chris@0 183 }
Chris@0 184
Chris@0 185 /**
Chris@0 186 * Gets the current step position.
Chris@0 187 *
Chris@0 188 * @return int The progress bar step
Chris@0 189 */
Chris@0 190 public function getProgress()
Chris@0 191 {
Chris@0 192 return $this->step;
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * Gets the progress bar step width.
Chris@0 197 *
Chris@0 198 * @return int The progress bar step width
Chris@0 199 */
Chris@0 200 private function getStepWidth()
Chris@0 201 {
Chris@0 202 return $this->stepWidth;
Chris@0 203 }
Chris@0 204
Chris@0 205 /**
Chris@0 206 * Gets the current progress bar percent.
Chris@0 207 *
Chris@0 208 * @return float The current progress bar percent
Chris@0 209 */
Chris@0 210 public function getProgressPercent()
Chris@0 211 {
Chris@0 212 return $this->percent;
Chris@0 213 }
Chris@0 214
Chris@0 215 /**
Chris@0 216 * Sets the progress bar width.
Chris@0 217 *
Chris@0 218 * @param int $size The progress bar size
Chris@0 219 */
Chris@0 220 public function setBarWidth($size)
Chris@0 221 {
Chris@0 222 $this->barWidth = max(1, (int) $size);
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Gets the progress bar width.
Chris@0 227 *
Chris@0 228 * @return int The progress bar size
Chris@0 229 */
Chris@0 230 public function getBarWidth()
Chris@0 231 {
Chris@0 232 return $this->barWidth;
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Sets the bar character.
Chris@0 237 *
Chris@0 238 * @param string $char A character
Chris@0 239 */
Chris@0 240 public function setBarCharacter($char)
Chris@0 241 {
Chris@0 242 $this->barChar = $char;
Chris@0 243 }
Chris@0 244
Chris@0 245 /**
Chris@0 246 * Gets the bar character.
Chris@0 247 *
Chris@0 248 * @return string A character
Chris@0 249 */
Chris@0 250 public function getBarCharacter()
Chris@0 251 {
Chris@0 252 if (null === $this->barChar) {
Chris@0 253 return $this->max ? '=' : $this->emptyBarChar;
Chris@0 254 }
Chris@0 255
Chris@0 256 return $this->barChar;
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * Sets the empty bar character.
Chris@0 261 *
Chris@0 262 * @param string $char A character
Chris@0 263 */
Chris@0 264 public function setEmptyBarCharacter($char)
Chris@0 265 {
Chris@0 266 $this->emptyBarChar = $char;
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Gets the empty bar character.
Chris@0 271 *
Chris@0 272 * @return string A character
Chris@0 273 */
Chris@0 274 public function getEmptyBarCharacter()
Chris@0 275 {
Chris@0 276 return $this->emptyBarChar;
Chris@0 277 }
Chris@0 278
Chris@0 279 /**
Chris@0 280 * Sets the progress bar character.
Chris@0 281 *
Chris@0 282 * @param string $char A character
Chris@0 283 */
Chris@0 284 public function setProgressCharacter($char)
Chris@0 285 {
Chris@0 286 $this->progressChar = $char;
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Gets the progress bar character.
Chris@0 291 *
Chris@0 292 * @return string A character
Chris@0 293 */
Chris@0 294 public function getProgressCharacter()
Chris@0 295 {
Chris@0 296 return $this->progressChar;
Chris@0 297 }
Chris@0 298
Chris@0 299 /**
Chris@0 300 * Sets the progress bar format.
Chris@0 301 *
Chris@0 302 * @param string $format The format
Chris@0 303 */
Chris@0 304 public function setFormat($format)
Chris@0 305 {
Chris@0 306 $this->format = null;
Chris@0 307 $this->internalFormat = $format;
Chris@0 308 }
Chris@0 309
Chris@0 310 /**
Chris@0 311 * Sets the redraw frequency.
Chris@0 312 *
Chris@0 313 * @param int|float $freq The frequency in steps
Chris@0 314 */
Chris@0 315 public function setRedrawFrequency($freq)
Chris@0 316 {
Chris@0 317 $this->redrawFreq = max((int) $freq, 1);
Chris@0 318 }
Chris@0 319
Chris@0 320 /**
Chris@0 321 * Starts the progress output.
Chris@0 322 *
Chris@0 323 * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged
Chris@0 324 */
Chris@0 325 public function start($max = null)
Chris@0 326 {
Chris@0 327 $this->startTime = time();
Chris@0 328 $this->step = 0;
Chris@0 329 $this->percent = 0.0;
Chris@0 330
Chris@0 331 if (null !== $max) {
Chris@0 332 $this->setMaxSteps($max);
Chris@0 333 }
Chris@0 334
Chris@0 335 $this->display();
Chris@0 336 }
Chris@0 337
Chris@0 338 /**
Chris@0 339 * Advances the progress output X steps.
Chris@0 340 *
Chris@0 341 * @param int $step Number of steps to advance
Chris@0 342 */
Chris@0 343 public function advance($step = 1)
Chris@0 344 {
Chris@0 345 $this->setProgress($this->step + $step);
Chris@0 346 }
Chris@0 347
Chris@0 348 /**
Chris@0 349 * Sets whether to overwrite the progressbar, false for new line.
Chris@0 350 *
Chris@0 351 * @param bool $overwrite
Chris@0 352 */
Chris@0 353 public function setOverwrite($overwrite)
Chris@0 354 {
Chris@0 355 $this->overwrite = (bool) $overwrite;
Chris@0 356 }
Chris@0 357
Chris@0 358 /**
Chris@0 359 * Sets the current progress.
Chris@0 360 *
Chris@0 361 * @param int $step The current progress
Chris@0 362 */
Chris@0 363 public function setProgress($step)
Chris@0 364 {
Chris@0 365 $step = (int) $step;
Chris@0 366
Chris@0 367 if ($this->max && $step > $this->max) {
Chris@0 368 $this->max = $step;
Chris@0 369 } elseif ($step < 0) {
Chris@0 370 $step = 0;
Chris@0 371 }
Chris@0 372
Chris@0 373 $prevPeriod = (int) ($this->step / $this->redrawFreq);
Chris@0 374 $currPeriod = (int) ($step / $this->redrawFreq);
Chris@0 375 $this->step = $step;
Chris@0 376 $this->percent = $this->max ? (float) $this->step / $this->max : 0;
Chris@0 377 if ($prevPeriod !== $currPeriod || $this->max === $step) {
Chris@0 378 $this->display();
Chris@0 379 }
Chris@0 380 }
Chris@0 381
Chris@0 382 /**
Chris@0 383 * Finishes the progress output.
Chris@0 384 */
Chris@0 385 public function finish()
Chris@0 386 {
Chris@0 387 if (!$this->max) {
Chris@0 388 $this->max = $this->step;
Chris@0 389 }
Chris@0 390
Chris@0 391 if ($this->step === $this->max && !$this->overwrite) {
Chris@0 392 // prevent double 100% output
Chris@0 393 return;
Chris@0 394 }
Chris@0 395
Chris@0 396 $this->setProgress($this->max);
Chris@0 397 }
Chris@0 398
Chris@0 399 /**
Chris@0 400 * Outputs the current progress string.
Chris@0 401 */
Chris@0 402 public function display()
Chris@0 403 {
Chris@0 404 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
Chris@0 405 return;
Chris@0 406 }
Chris@0 407
Chris@0 408 if (null === $this->format) {
Chris@0 409 $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
Chris@0 410 }
Chris@0 411
Chris@0 412 $this->overwrite($this->buildLine());
Chris@0 413 }
Chris@0 414
Chris@0 415 /**
Chris@0 416 * Removes the progress bar from the current line.
Chris@0 417 *
Chris@0 418 * This is useful if you wish to write some output
Chris@0 419 * while a progress bar is running.
Chris@0 420 * Call display() to show the progress bar again.
Chris@0 421 */
Chris@0 422 public function clear()
Chris@0 423 {
Chris@0 424 if (!$this->overwrite) {
Chris@0 425 return;
Chris@0 426 }
Chris@0 427
Chris@0 428 if (null === $this->format) {
Chris@0 429 $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
Chris@0 430 }
Chris@0 431
Chris@0 432 $this->overwrite('');
Chris@0 433 }
Chris@0 434
Chris@0 435 /**
Chris@0 436 * Sets the progress bar format.
Chris@0 437 *
Chris@0 438 * @param string $format The format
Chris@0 439 */
Chris@0 440 private function setRealFormat($format)
Chris@0 441 {
Chris@0 442 // try to use the _nomax variant if available
Chris@0 443 if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
Chris@0 444 $this->format = self::getFormatDefinition($format.'_nomax');
Chris@0 445 } elseif (null !== self::getFormatDefinition($format)) {
Chris@0 446 $this->format = self::getFormatDefinition($format);
Chris@0 447 } else {
Chris@0 448 $this->format = $format;
Chris@0 449 }
Chris@0 450
Chris@0 451 $this->formatLineCount = substr_count($this->format, "\n");
Chris@0 452 }
Chris@0 453
Chris@0 454 /**
Chris@0 455 * Sets the progress bar maximal steps.
Chris@0 456 *
Chris@0 457 * @param int $max The progress bar max steps
Chris@0 458 */
Chris@0 459 private function setMaxSteps($max)
Chris@0 460 {
Chris@0 461 $this->max = max(0, (int) $max);
Chris@0 462 $this->stepWidth = $this->max ? Helper::strlen($this->max) : 4;
Chris@0 463 }
Chris@0 464
Chris@0 465 /**
Chris@0 466 * Overwrites a previous message to the output.
Chris@0 467 *
Chris@0 468 * @param string $message The message
Chris@0 469 */
Chris@0 470 private function overwrite($message)
Chris@0 471 {
Chris@0 472 if ($this->overwrite) {
Chris@0 473 if (!$this->firstRun) {
Chris@0 474 // Move the cursor to the beginning of the line
Chris@0 475 $this->output->write("\x0D");
Chris@0 476
Chris@0 477 // Erase the line
Chris@0 478 $this->output->write("\x1B[2K");
Chris@0 479
Chris@0 480 // Erase previous lines
Chris@0 481 if ($this->formatLineCount > 0) {
Chris@0 482 $this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
Chris@0 483 }
Chris@0 484 }
Chris@0 485 } elseif ($this->step > 0) {
Chris@0 486 $this->output->writeln('');
Chris@0 487 }
Chris@0 488
Chris@0 489 $this->firstRun = false;
Chris@0 490
Chris@0 491 $this->output->write($message);
Chris@0 492 }
Chris@0 493
Chris@0 494 private function determineBestFormat()
Chris@0 495 {
Chris@0 496 switch ($this->output->getVerbosity()) {
Chris@0 497 // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
Chris@0 498 case OutputInterface::VERBOSITY_VERBOSE:
Chris@0 499 return $this->max ? 'verbose' : 'verbose_nomax';
Chris@0 500 case OutputInterface::VERBOSITY_VERY_VERBOSE:
Chris@0 501 return $this->max ? 'very_verbose' : 'very_verbose_nomax';
Chris@0 502 case OutputInterface::VERBOSITY_DEBUG:
Chris@0 503 return $this->max ? 'debug' : 'debug_nomax';
Chris@0 504 default:
Chris@0 505 return $this->max ? 'normal' : 'normal_nomax';
Chris@0 506 }
Chris@0 507 }
Chris@0 508
Chris@0 509 private static function initPlaceholderFormatters()
Chris@0 510 {
Chris@0 511 return array(
Chris@0 512 'bar' => function (ProgressBar $bar, OutputInterface $output) {
Chris@0 513 $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth());
Chris@0 514 $display = str_repeat($bar->getBarCharacter(), $completeBars);
Chris@0 515 if ($completeBars < $bar->getBarWidth()) {
Chris@0 516 $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
Chris@0 517 $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
Chris@0 518 }
Chris@0 519
Chris@0 520 return $display;
Chris@0 521 },
Chris@0 522 'elapsed' => function (ProgressBar $bar) {
Chris@0 523 return Helper::formatTime(time() - $bar->getStartTime());
Chris@0 524 },
Chris@0 525 'remaining' => function (ProgressBar $bar) {
Chris@0 526 if (!$bar->getMaxSteps()) {
Chris@0 527 throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
Chris@0 528 }
Chris@0 529
Chris@0 530 if (!$bar->getProgress()) {
Chris@0 531 $remaining = 0;
Chris@0 532 } else {
Chris@0 533 $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()));
Chris@0 534 }
Chris@0 535
Chris@0 536 return Helper::formatTime($remaining);
Chris@0 537 },
Chris@0 538 'estimated' => function (ProgressBar $bar) {
Chris@0 539 if (!$bar->getMaxSteps()) {
Chris@0 540 throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
Chris@0 541 }
Chris@0 542
Chris@0 543 if (!$bar->getProgress()) {
Chris@0 544 $estimated = 0;
Chris@0 545 } else {
Chris@0 546 $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
Chris@0 547 }
Chris@0 548
Chris@0 549 return Helper::formatTime($estimated);
Chris@0 550 },
Chris@0 551 'memory' => function (ProgressBar $bar) {
Chris@0 552 return Helper::formatMemory(memory_get_usage(true));
Chris@0 553 },
Chris@0 554 'current' => function (ProgressBar $bar) {
Chris@0 555 return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
Chris@0 556 },
Chris@0 557 'max' => function (ProgressBar $bar) {
Chris@0 558 return $bar->getMaxSteps();
Chris@0 559 },
Chris@0 560 'percent' => function (ProgressBar $bar) {
Chris@0 561 return floor($bar->getProgressPercent() * 100);
Chris@0 562 },
Chris@0 563 );
Chris@0 564 }
Chris@0 565
Chris@0 566 private static function initFormats()
Chris@0 567 {
Chris@0 568 return array(
Chris@0 569 'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
Chris@0 570 'normal_nomax' => ' %current% [%bar%]',
Chris@0 571
Chris@0 572 'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
Chris@0 573 'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
Chris@0 574
Chris@0 575 'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%',
Chris@0 576 'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
Chris@0 577
Chris@0 578 'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
Chris@0 579 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
Chris@0 580 );
Chris@0 581 }
Chris@0 582
Chris@0 583 /**
Chris@0 584 * @return string
Chris@0 585 */
Chris@0 586 private function buildLine()
Chris@0 587 {
Chris@0 588 $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
Chris@0 589 $callback = function ($matches) {
Chris@0 590 if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
Chris@0 591 $text = call_user_func($formatter, $this, $this->output);
Chris@0 592 } elseif (isset($this->messages[$matches[1]])) {
Chris@0 593 $text = $this->messages[$matches[1]];
Chris@0 594 } else {
Chris@0 595 return $matches[0];
Chris@0 596 }
Chris@0 597
Chris@0 598 if (isset($matches[2])) {
Chris@0 599 $text = sprintf('%'.$matches[2], $text);
Chris@0 600 }
Chris@0 601
Chris@0 602 return $text;
Chris@0 603 };
Chris@0 604 $line = preg_replace_callback($regex, $callback, $this->format);
Chris@0 605
Chris@0 606 // gets string length for each sub line with multiline format
Chris@0 607 $linesLength = array_map(function ($subLine) {
Chris@0 608 return Helper::strlenWithoutDecoration($this->output->getFormatter(), rtrim($subLine, "\r"));
Chris@0 609 }, explode("\n", $line));
Chris@0 610
Chris@0 611 $linesWidth = max($linesLength);
Chris@0 612
Chris@0 613 $terminalWidth = $this->terminal->getWidth();
Chris@0 614 if ($linesWidth <= $terminalWidth) {
Chris@0 615 return $line;
Chris@0 616 }
Chris@0 617
Chris@0 618 $this->setBarWidth($this->barWidth - $linesWidth + $terminalWidth);
Chris@0 619
Chris@0 620 return preg_replace_callback($regex, $callback, $this->format);
Chris@0 621 }
Chris@0 622 }