comparison vendor/symfony/console/Helper/ProgressBar.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Console\Helper; 12 namespace Symfony\Component\Console\Helper;
13 13
14 use Symfony\Component\Console\Exception\LogicException;
14 use Symfony\Component\Console\Output\ConsoleOutputInterface; 15 use Symfony\Component\Console\Output\ConsoleOutputInterface;
15 use Symfony\Component\Console\Output\OutputInterface; 16 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Console\Exception\LogicException;
17 use Symfony\Component\Console\Terminal; 17 use Symfony\Component\Console\Terminal;
18 18
19 /** 19 /**
20 * The ProgressBar provides helpers to display progress output. 20 * The ProgressBar provides helpers to display progress output.
21 * 21 *
36 private $max; 36 private $max;
37 private $startTime; 37 private $startTime;
38 private $stepWidth; 38 private $stepWidth;
39 private $percent = 0.0; 39 private $percent = 0.0;
40 private $formatLineCount; 40 private $formatLineCount;
41 private $messages = array(); 41 private $messages = [];
42 private $overwrite = true; 42 private $overwrite = true;
43 private $terminal; 43 private $terminal;
44 private $firstRun = true; 44 private $firstRun = true;
45 45
46 private static $formatters; 46 private static $formatters;
501 } 501 }
502 } 502 }
503 503
504 private static function initPlaceholderFormatters() 504 private static function initPlaceholderFormatters()
505 { 505 {
506 return array( 506 return [
507 'bar' => function (ProgressBar $bar, OutputInterface $output) { 507 'bar' => function (self $bar, OutputInterface $output) {
508 $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth()); 508 $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth());
509 $display = str_repeat($bar->getBarCharacter(), $completeBars); 509 $display = str_repeat($bar->getBarCharacter(), $completeBars);
510 if ($completeBars < $bar->getBarWidth()) { 510 if ($completeBars < $bar->getBarWidth()) {
511 $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter()); 511 $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
512 $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars); 512 $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
513 } 513 }
514 514
515 return $display; 515 return $display;
516 }, 516 },
517 'elapsed' => function (ProgressBar $bar) { 517 'elapsed' => function (self $bar) {
518 return Helper::formatTime(time() - $bar->getStartTime()); 518 return Helper::formatTime(time() - $bar->getStartTime());
519 }, 519 },
520 'remaining' => function (ProgressBar $bar) { 520 'remaining' => function (self $bar) {
521 if (!$bar->getMaxSteps()) { 521 if (!$bar->getMaxSteps()) {
522 throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.'); 522 throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
523 } 523 }
524 524
525 if (!$bar->getProgress()) { 525 if (!$bar->getProgress()) {
528 $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())); 528 $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()));
529 } 529 }
530 530
531 return Helper::formatTime($remaining); 531 return Helper::formatTime($remaining);
532 }, 532 },
533 'estimated' => function (ProgressBar $bar) { 533 'estimated' => function (self $bar) {
534 if (!$bar->getMaxSteps()) { 534 if (!$bar->getMaxSteps()) {
535 throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.'); 535 throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
536 } 536 }
537 537
538 if (!$bar->getProgress()) { 538 if (!$bar->getProgress()) {
541 $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps()); 541 $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
542 } 542 }
543 543
544 return Helper::formatTime($estimated); 544 return Helper::formatTime($estimated);
545 }, 545 },
546 'memory' => function (ProgressBar $bar) { 546 'memory' => function (self $bar) {
547 return Helper::formatMemory(memory_get_usage(true)); 547 return Helper::formatMemory(memory_get_usage(true));
548 }, 548 },
549 'current' => function (ProgressBar $bar) { 549 'current' => function (self $bar) {
550 return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT); 550 return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
551 }, 551 },
552 'max' => function (ProgressBar $bar) { 552 'max' => function (self $bar) {
553 return $bar->getMaxSteps(); 553 return $bar->getMaxSteps();
554 }, 554 },
555 'percent' => function (ProgressBar $bar) { 555 'percent' => function (self $bar) {
556 return floor($bar->getProgressPercent() * 100); 556 return floor($bar->getProgressPercent() * 100);
557 }, 557 },
558 ); 558 ];
559 } 559 }
560 560
561 private static function initFormats() 561 private static function initFormats()
562 { 562 {
563 return array( 563 return [
564 'normal' => ' %current%/%max% [%bar%] %percent:3s%%', 564 'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
565 'normal_nomax' => ' %current% [%bar%]', 565 'normal_nomax' => ' %current% [%bar%]',
566 566
567 'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%', 567 'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
568 'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%', 568 'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
570 'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%', 570 'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%',
571 'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%', 571 'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
572 572
573 'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%', 573 'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
574 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%', 574 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
575 ); 575 ];
576 } 576 }
577 577
578 /** 578 /**
579 * @return string 579 * @return string
580 */ 580 */
581 private function buildLine() 581 private function buildLine()
582 { 582 {
583 $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i"; 583 $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
584 $callback = function ($matches) { 584 $callback = function ($matches) {
585 if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) { 585 if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
586 $text = call_user_func($formatter, $this, $this->output); 586 $text = \call_user_func($formatter, $this, $this->output);
587 } elseif (isset($this->messages[$matches[1]])) { 587 } elseif (isset($this->messages[$matches[1]])) {
588 $text = $this->messages[$matches[1]]; 588 $text = $this->messages[$matches[1]];
589 } else { 589 } else {
590 return $matches[0]; 590 return $matches[0];
591 } 591 }