2014-03-01 04:11:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Console\Helper;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ProgressBar provides helpers to display progress output.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
* @author Chris Jones <leeked@gmail.com>
|
|
|
|
*/
|
|
|
|
class ProgressBar
|
|
|
|
{
|
|
|
|
// options
|
|
|
|
private $barWidth = 28;
|
|
|
|
private $barChar = '=';
|
|
|
|
private $emptyBarChar = '-';
|
|
|
|
private $progressChar = '>';
|
|
|
|
private $format = null;
|
|
|
|
private $redrawFreq = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var OutputInterface
|
|
|
|
*/
|
|
|
|
private $output;
|
|
|
|
private $step;
|
|
|
|
private $max;
|
|
|
|
private $startTime;
|
2014-03-01 07:26:09 +00:00
|
|
|
private $stepWidth;
|
|
|
|
private $percent;
|
2014-03-01 04:11:27 +00:00
|
|
|
private $lastMessagesLength;
|
|
|
|
private $barCharOriginal;
|
2014-03-01 07:54:45 +00:00
|
|
|
private $formatLineCount;
|
2014-03-01 08:14:29 +00:00
|
|
|
private $messages;
|
2014-03-01 04:11:27 +00:00
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
private static $formatters;
|
|
|
|
private static $formats;
|
2014-03-01 07:26:09 +00:00
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
2014-03-01 07:26:09 +00:00
|
|
|
* Constructor.
|
2014-03-01 04:11:27 +00:00
|
|
|
*
|
2014-03-01 07:26:09 +00:00
|
|
|
* @param OutputInterface $output An OutputInterface instance
|
|
|
|
* @param integer $max Maximum steps (0 if unknown)
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
2014-03-01 07:26:09 +00:00
|
|
|
public function __construct(OutputInterface $output, $max = 0)
|
|
|
|
{
|
|
|
|
// Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
|
|
|
|
$this->output = $output->isDecorated() ? $output : new NullOutput();
|
|
|
|
$this->max = (int) $max;
|
|
|
|
$this->stepWidth = $this->max > 0 ? Helper::strlen($this->max) : 4;
|
|
|
|
|
|
|
|
if (!self::$formatters) {
|
|
|
|
self::$formatters = self::initPlaceholderFormatters();
|
|
|
|
}
|
2014-03-01 16:52:22 +00:00
|
|
|
|
|
|
|
if (!self::$formats) {
|
|
|
|
self::$formats = self::initFormats();
|
|
|
|
}
|
2014-03-03 09:33:57 +00:00
|
|
|
|
|
|
|
$this->setFormat($this->determineBestFormat());
|
2014-03-01 07:26:09 +00:00
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-01 07:26:09 +00:00
|
|
|
* Sets a placeholder formatter for a given name.
|
|
|
|
*
|
|
|
|
* This method also allow you to override an existing placeholder.
|
2014-03-01 04:11:27 +00:00
|
|
|
*
|
2014-03-01 07:26:09 +00:00
|
|
|
* @param string $name The placeholder name (including the delimiter char like %)
|
|
|
|
* @param callable $callable A PHP callable
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
2014-03-01 16:52:22 +00:00
|
|
|
public static function setPlaceholderFormatterDefinition($name, $callable)
|
2014-03-01 07:26:09 +00:00
|
|
|
{
|
|
|
|
if (!self::$formatters) {
|
|
|
|
self::$formatters = self::initPlaceholderFormatters();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$formatters[$name] = $callable;
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
/**
|
|
|
|
* Gets the placeholder formatter for a given name.
|
|
|
|
*
|
|
|
|
* @param string $name The placeholder name (including the delimiter char like %)
|
|
|
|
*
|
|
|
|
* @return callable|null A PHP callable
|
|
|
|
*/
|
|
|
|
public static function getPlaceholderFormatterDefinition($name)
|
|
|
|
{
|
2014-03-03 12:22:12 +00:00
|
|
|
if (!self::$formatters) {
|
|
|
|
self::$formatters = self::initPlaceholderFormatters();
|
|
|
|
}
|
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
|
|
|
|
}
|
|
|
|
|
2014-03-01 16:52:22 +00:00
|
|
|
/**
|
|
|
|
* Sets a format for a given name.
|
|
|
|
*
|
|
|
|
* This method also allow you to override an existing format.
|
|
|
|
*
|
|
|
|
* @param string $name The format name
|
|
|
|
* @param string $format A format string
|
|
|
|
*/
|
|
|
|
public static function setFormatDefinition($name, $format)
|
|
|
|
{
|
|
|
|
if (!self::$formats) {
|
|
|
|
self::$formats = self::initFormats();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$formats[$name] = $format;
|
|
|
|
}
|
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
/**
|
|
|
|
* Gets the format for a given name.
|
|
|
|
*
|
|
|
|
* @param string $name The format name
|
|
|
|
*
|
|
|
|
* @return string|null A format string
|
|
|
|
*/
|
|
|
|
public static function getFormatDefinition($name)
|
|
|
|
{
|
2014-03-03 12:22:12 +00:00
|
|
|
if (!self::$formats) {
|
|
|
|
self::$formats = self::initFormats();
|
|
|
|
}
|
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
return isset(self::$formats[$name]) ? self::$formats[$name] : null;
|
|
|
|
}
|
|
|
|
|
2014-03-01 08:14:29 +00:00
|
|
|
public function setMessage($message, $name = 'message')
|
|
|
|
{
|
|
|
|
$this->messages[$name] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMessage($name = 'message')
|
|
|
|
{
|
|
|
|
return $this->messages[$name];
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
2014-03-01 07:26:09 +00:00
|
|
|
* Gets the progress bar start time.
|
2014-03-01 04:11:27 +00:00
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The progress bar start time
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
2014-03-01 07:26:09 +00:00
|
|
|
public function getStartTime()
|
|
|
|
{
|
|
|
|
return $this->startTime;
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
|
2014-03-01 07:26:09 +00:00
|
|
|
/**
|
|
|
|
* Gets the progress bar maximal steps.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The progress bar max steps
|
2014-03-01 07:26:09 +00:00
|
|
|
*/
|
|
|
|
public function getMaxSteps()
|
|
|
|
{
|
|
|
|
return $this->max;
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-01 07:26:09 +00:00
|
|
|
* Gets the progress bar step.
|
2014-03-01 04:11:27 +00:00
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The progress bar step
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
2014-03-01 07:26:09 +00:00
|
|
|
public function getStep()
|
2014-03-01 04:11:27 +00:00
|
|
|
{
|
2014-03-01 07:26:09 +00:00
|
|
|
return $this->step;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the progress bar step width.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The progress bar step width
|
2014-03-01 07:26:09 +00:00
|
|
|
*/
|
|
|
|
public function getStepWidth()
|
|
|
|
{
|
|
|
|
return $this->stepWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current progress bar percent.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The current progress bar percent
|
2014-03-01 07:26:09 +00:00
|
|
|
*/
|
|
|
|
public function getProgressPercent()
|
|
|
|
{
|
|
|
|
return $this->percent;
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the progress bar width.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @param integer $size The progress bar size
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
|
|
|
public function setBarWidth($size)
|
|
|
|
{
|
|
|
|
$this->barWidth = (int) $size;
|
|
|
|
}
|
|
|
|
|
2014-03-01 07:26:09 +00:00
|
|
|
/**
|
|
|
|
* Gets the progress bar width.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @return integer The progress bar size
|
2014-03-01 07:26:09 +00:00
|
|
|
*/
|
|
|
|
public function getBarWidth()
|
|
|
|
{
|
|
|
|
return $this->barWidth;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the bar character.
|
|
|
|
*
|
|
|
|
* @param string $char A character
|
|
|
|
*/
|
|
|
|
public function setBarCharacter($char)
|
|
|
|
{
|
|
|
|
$this->barChar = $char;
|
|
|
|
}
|
|
|
|
|
2014-03-01 07:26:09 +00:00
|
|
|
/**
|
|
|
|
* Gets the bar character.
|
|
|
|
*
|
|
|
|
* @return string A character
|
|
|
|
*/
|
|
|
|
public function getBarCharacter()
|
|
|
|
{
|
|
|
|
return $this->barChar;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the empty bar character.
|
|
|
|
*
|
|
|
|
* @param string $char A character
|
|
|
|
*/
|
|
|
|
public function setEmptyBarCharacter($char)
|
|
|
|
{
|
|
|
|
$this->emptyBarChar = $char;
|
|
|
|
}
|
|
|
|
|
2014-03-01 07:26:09 +00:00
|
|
|
/**
|
|
|
|
* Gets the empty bar character.
|
|
|
|
*
|
|
|
|
* @return string A character
|
|
|
|
*/
|
|
|
|
public function getEmptyBarCharacter()
|
|
|
|
{
|
|
|
|
return $this->emptyBarChar;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the progress bar character.
|
|
|
|
*
|
|
|
|
* @param string $char A character
|
|
|
|
*/
|
|
|
|
public function setProgressCharacter($char)
|
|
|
|
{
|
|
|
|
$this->progressChar = $char;
|
|
|
|
}
|
|
|
|
|
2014-03-01 07:26:09 +00:00
|
|
|
/**
|
|
|
|
* Gets the progress bar character.
|
|
|
|
*
|
|
|
|
* @return string A character
|
|
|
|
*/
|
|
|
|
public function getProgressCharacter()
|
|
|
|
{
|
|
|
|
return $this->progressChar;
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:11:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the progress bar format.
|
|
|
|
*
|
|
|
|
* @param string $format The format
|
|
|
|
*/
|
|
|
|
public function setFormat($format)
|
|
|
|
{
|
2014-03-03 09:33:57 +00:00
|
|
|
// try to use the _nomax variant if available
|
|
|
|
if (!$this->max && isset(self::$formats[$format.'_nomax'])) {
|
|
|
|
$this->format = self::$formats[$format.'_nomax'];
|
|
|
|
} elseif (isset(self::$formats[$format])) {
|
|
|
|
$this->format = self::$formats[$format];
|
|
|
|
} else {
|
|
|
|
$this->format = $format;
|
|
|
|
}
|
|
|
|
|
2014-03-01 16:52:22 +00:00
|
|
|
$this->formatLineCount = substr_count($this->format, "\n");
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the redraw frequency.
|
|
|
|
*
|
2014-03-01 17:08:18 +00:00
|
|
|
* @param integer $freq The frequency in steps
|
2014-03-01 04:11:27 +00:00
|
|
|
*/
|
|
|
|
public function setRedrawFrequency($freq)
|
|
|
|
{
|
|
|
|
$this->redrawFreq = (int) $freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the progress output.
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
$this->startTime = time();
|
|
|
|
$this->step = 0;
|
|
|
|
$this->percent = 0;
|
|
|
|
$this->lastMessagesLength = 0;
|
|
|
|
$this->barCharOriginal = '';
|
|
|
|
|
|
|
|
if (!$this->max) {
|
|
|
|
$this->barCharOriginal = $this->barChar;
|
|
|
|
$this->barChar = $this->emptyBarChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the progress output X steps.
|
|
|
|
*
|
|
|
|
* @param integer $step Number of steps to advance
|
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
|
|
|
public function advance($step = 1)
|
|
|
|
{
|
|
|
|
$this->setCurrent($this->step + $step);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current progress.
|
|
|
|
*
|
|
|
|
* @param integer $step The current progress
|
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
|
|
|
public function setCurrent($step)
|
|
|
|
{
|
|
|
|
if (null === $this->startTime) {
|
|
|
|
throw new \LogicException('You must start the progress bar before calling setCurrent().');
|
|
|
|
}
|
|
|
|
|
|
|
|
$step = (int) $step;
|
|
|
|
if ($step < $this->step) {
|
|
|
|
throw new \LogicException('You can\'t regress the progress bar.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->max > 0 && $step > $this->max) {
|
|
|
|
throw new \LogicException('You can\'t advance the progress bar past the max value.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$prevPeriod = intval($this->step / $this->redrawFreq);
|
|
|
|
$currPeriod = intval($step / $this->redrawFreq);
|
|
|
|
$this->step = $step;
|
|
|
|
$this->percent = $this->max > 0 ? (float) $this->step / $this->max : 0;
|
|
|
|
if ($prevPeriod !== $currPeriod || $this->max === $step) {
|
|
|
|
$this->display();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finishes the progress output.
|
|
|
|
*/
|
|
|
|
public function finish()
|
|
|
|
{
|
|
|
|
if (null === $this->startTime) {
|
|
|
|
throw new \LogicException('You must start the progress bar before calling finish().');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->max) {
|
|
|
|
$this->barChar = $this->barCharOriginal;
|
|
|
|
$this->max = $this->step;
|
|
|
|
$this->setCurrent($this->max);
|
|
|
|
$this->max = 0;
|
|
|
|
$this->barChar = $this->emptyBarChar;
|
|
|
|
} else {
|
|
|
|
$this->setCurrent($this->max);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->startTime = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the current progress string.
|
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
|
|
|
public function display()
|
|
|
|
{
|
|
|
|
if (null === $this->startTime) {
|
|
|
|
throw new \LogicException('You must start the progress bar before calling display().');
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:26:36 +00:00
|
|
|
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
|
2014-03-01 07:26:09 +00:00
|
|
|
$self = $this;
|
2014-03-01 21:26:36 +00:00
|
|
|
$output = $this->output;
|
|
|
|
$messages = $this->messages;
|
|
|
|
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self, $output, $messages) {
|
2014-03-01 17:08:18 +00:00
|
|
|
if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
|
2014-03-01 21:26:36 +00:00
|
|
|
$text = call_user_func($formatter, $self, $output);
|
|
|
|
} elseif (isset($messages[$matches[1]])) {
|
|
|
|
$text = $messages[$matches[1]];
|
2014-03-01 08:14:29 +00:00
|
|
|
} else {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($matches[2])) {
|
|
|
|
$text = sprintf('%'.$matches[2], $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
2014-03-01 07:26:09 +00:00
|
|
|
}, $this->format));
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the progress bar from the current line.
|
|
|
|
*
|
|
|
|
* This is useful if you wish to write some output
|
|
|
|
* while a progress bar is running.
|
|
|
|
* Call display() to show the progress bar again.
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
2014-03-01 07:54:45 +00:00
|
|
|
$this->overwrite(str_repeat("\n", $this->formatLineCount));
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrites a previous message to the output.
|
|
|
|
*
|
|
|
|
* @param string $message The message
|
|
|
|
*/
|
|
|
|
private function overwrite($message)
|
|
|
|
{
|
2014-03-01 21:26:36 +00:00
|
|
|
$lines = explode("\n", $message);
|
2014-03-01 04:11:27 +00:00
|
|
|
|
2014-03-01 21:26:36 +00:00
|
|
|
// append whitespace to match the line's length
|
|
|
|
if (null !== $this->lastMessagesLength) {
|
|
|
|
foreach ($lines as $i => $line) {
|
|
|
|
if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $line)) {
|
|
|
|
$lines[$i] = str_pad($line, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 07:54:45 +00:00
|
|
|
// move back to the beginning of the progress bar before redrawing it
|
2014-03-01 04:11:27 +00:00
|
|
|
$this->output->write("\x0D");
|
2014-03-01 07:54:45 +00:00
|
|
|
if ($this->formatLineCount) {
|
|
|
|
$this->output->write(sprintf("\033[%dA", $this->formatLineCount));
|
|
|
|
}
|
2014-03-01 21:26:36 +00:00
|
|
|
$this->output->write(implode("\n", $lines));
|
2014-03-01 04:11:27 +00:00
|
|
|
|
2014-03-01 21:26:36 +00:00
|
|
|
$this->lastMessagesLength = 0;
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
|
|
|
|
if ($len > $this->lastMessagesLength) {
|
|
|
|
$this->lastMessagesLength = $len;
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function determineBestFormat()
|
|
|
|
{
|
|
|
|
switch ($this->output->getVerbosity()) {
|
2014-03-03 09:33:57 +00:00
|
|
|
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
|
2014-03-01 04:11:27 +00:00
|
|
|
case OutputInterface::VERBOSITY_VERBOSE:
|
2014-03-03 09:33:57 +00:00
|
|
|
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
|
2014-03-01 04:11:27 +00:00
|
|
|
case OutputInterface::VERBOSITY_VERY_VERBOSE:
|
2014-03-03 09:33:57 +00:00
|
|
|
return $this->max > 0 ? 'very_verbose' : 'very_verbose_nomax';
|
2014-03-01 04:11:27 +00:00
|
|
|
case OutputInterface::VERBOSITY_DEBUG:
|
2014-03-03 09:33:57 +00:00
|
|
|
return $this->max > 0 ? 'debug' : 'debug_nomax';
|
2014-03-01 04:11:27 +00:00
|
|
|
default:
|
2014-03-01 16:52:22 +00:00
|
|
|
return $this->max > 0 ? 'normal' : 'normal_nomax';
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-01 07:26:09 +00:00
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
private static function initPlaceholderFormatters()
|
2014-03-01 07:26:09 +00:00
|
|
|
{
|
|
|
|
return array(
|
2014-03-01 21:26:36 +00:00
|
|
|
'bar' => function (ProgressBar $bar, OutputInterface $output) {
|
2014-03-01 07:26:09 +00:00
|
|
|
$completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getStep() % $bar->getBarWidth());
|
|
|
|
$display = str_repeat($bar->getBarCharacter(), $completeBars);
|
|
|
|
if ($completeBars < $bar->getBarWidth()) {
|
2014-03-01 21:26:36 +00:00
|
|
|
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
|
2014-03-01 07:26:09 +00:00
|
|
|
$display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $display;
|
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'elapsed' => function (ProgressBar $bar) {
|
|
|
|
return Helper::formatTime(time() - $bar->getStartTime());
|
2014-03-01 07:26:09 +00:00
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'remaining' => function (ProgressBar $bar) {
|
2014-03-01 07:26:32 +00:00
|
|
|
if (!$bar->getMaxSteps()) {
|
|
|
|
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bar->getStep()) {
|
|
|
|
$remaining = 0;
|
|
|
|
} else {
|
|
|
|
$remaining = round((time() - $bar->getStartTime()) / $bar->getStep() * ($bar->getMaxSteps() - $bar->getStep()));
|
|
|
|
}
|
|
|
|
|
2014-03-01 08:14:29 +00:00
|
|
|
return Helper::formatTime($remaining);
|
2014-03-01 07:26:32 +00:00
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'estimated' => function (ProgressBar $bar) {
|
2014-03-01 07:26:32 +00:00
|
|
|
if (!$bar->getMaxSteps()) {
|
|
|
|
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bar->getStep()) {
|
|
|
|
$estimated = 0;
|
|
|
|
} else {
|
|
|
|
$estimated = round((time() - $bar->getStartTime()) / $bar->getStep() * $bar->getMaxSteps());
|
|
|
|
}
|
|
|
|
|
2014-03-01 08:14:29 +00:00
|
|
|
return Helper::formatTime($estimated);
|
2014-03-01 07:26:32 +00:00
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'memory' => function (ProgressBar $bar) {
|
|
|
|
return Helper::formatMemory(memory_get_usage(true));
|
2014-03-01 07:26:32 +00:00
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'current' => function (ProgressBar $bar) {
|
2014-03-01 07:26:09 +00:00
|
|
|
return str_pad($bar->getStep(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
|
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'max' => function (ProgressBar $bar) {
|
2014-03-01 07:26:09 +00:00
|
|
|
return $bar->getMaxSteps();
|
|
|
|
},
|
2014-03-01 08:14:29 +00:00
|
|
|
'percent' => function (ProgressBar $bar) {
|
|
|
|
return floor($bar->getProgressPercent() * 100);
|
2014-03-01 07:26:09 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2014-03-01 16:52:22 +00:00
|
|
|
|
2014-03-01 17:08:18 +00:00
|
|
|
private static function initFormats()
|
2014-03-01 16:52:22 +00:00
|
|
|
{
|
|
|
|
return array(
|
2014-03-03 09:33:57 +00:00
|
|
|
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
|
|
|
|
'normal_nomax' => ' %current% [%bar%]',
|
|
|
|
|
|
|
|
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
|
|
|
|
'verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',
|
|
|
|
|
|
|
|
'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
|
|
|
|
'very_verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',
|
|
|
|
|
|
|
|
'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
|
|
|
|
'debug_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
|
2014-03-01 16:52:22 +00:00
|
|
|
);
|
|
|
|
}
|
2014-03-01 04:11:27 +00:00
|
|
|
}
|