Heimdall/vendor/symfony/console/Helper/ProgressIndicator.php

250 lines
7.1 KiB
PHP
Raw Normal View History

2018-02-01 20:01:12 +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\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class ProgressIndicator
{
2022-03-10 11:54:29 +00:00
private const FORMATS = [
'normal' => ' %indicator% %message%',
'normal_no_ansi' => ' %message%',
'verbose' => ' %indicator% %message% (%elapsed:6s%)',
'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
];
2018-02-01 20:01:12 +00:00
private $output;
private $startTime;
private $format;
private $message;
private $indicatorValues;
private $indicatorCurrent;
private $indicatorChangeInterval;
private $indicatorUpdateTime;
private $started = false;
2022-03-10 11:54:29 +00:00
/**
* @var array<string, callable>
*/
2018-02-01 20:01:12 +00:00
private static $formatters;
/**
2022-03-10 11:54:29 +00:00
* @param int $indicatorChangeInterval Change interval in milliseconds
* @param array|null $indicatorValues Animated indicator characters
2018-02-01 20:01:12 +00:00
*/
public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null)
2018-02-01 20:01:12 +00:00
{
$this->output = $output;
if (null === $format) {
$format = $this->determineBestFormat();
}
if (null === $indicatorValues) {
2019-06-11 11:29:32 +00:00
$indicatorValues = ['-', '\\', '|', '/'];
2018-02-01 20:01:12 +00:00
}
$indicatorValues = array_values($indicatorValues);
if (2 > \count($indicatorValues)) {
2018-02-01 20:01:12 +00:00
throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
}
$this->format = self::getFormatDefinition($format);
$this->indicatorChangeInterval = $indicatorChangeInterval;
$this->indicatorValues = $indicatorValues;
$this->startTime = time();
}
/**
* Sets the current indicator message.
*/
2022-03-10 11:54:29 +00:00
public function setMessage(?string $message)
2018-02-01 20:01:12 +00:00
{
$this->message = $message;
$this->display();
}
/**
* Starts the indicator output.
*/
2022-03-10 11:54:29 +00:00
public function start(string $message)
2018-02-01 20:01:12 +00:00
{
if ($this->started) {
throw new LogicException('Progress indicator already started.');
}
$this->message = $message;
$this->started = true;
$this->startTime = time();
$this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
$this->indicatorCurrent = 0;
$this->display();
}
/**
* Advances the indicator.
*/
public function advance()
{
if (!$this->started) {
throw new LogicException('Progress indicator has not yet been started.');
}
if (!$this->output->isDecorated()) {
return;
}
$currentTime = $this->getCurrentTimeInMilliseconds();
if ($currentTime < $this->indicatorUpdateTime) {
return;
}
$this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
++$this->indicatorCurrent;
$this->display();
}
/**
* Finish the indicator with message.
*
* @param $message
*/
2022-03-10 11:54:29 +00:00
public function finish(string $message)
2018-02-01 20:01:12 +00:00
{
if (!$this->started) {
throw new LogicException('Progress indicator has not yet been started.');
}
$this->message = $message;
$this->display();
$this->output->writeln('');
$this->started = false;
}
/**
* Gets the format for a given name.
*
2022-03-10 11:54:29 +00:00
* @return string|null
2018-02-01 20:01:12 +00:00
*/
2022-03-10 11:54:29 +00:00
public static function getFormatDefinition(string $name)
2018-02-01 20:01:12 +00:00
{
2022-03-10 11:54:29 +00:00
return self::FORMATS[$name] ?? null;
2018-02-01 20:01:12 +00:00
}
/**
* Sets a placeholder formatter for a given name.
*
* This method also allow you to override an existing placeholder.
*/
2022-03-10 11:54:29 +00:00
public static function setPlaceholderFormatterDefinition(string $name, callable $callable)
2018-02-01 20:01:12 +00:00
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
}
self::$formatters[$name] = $callable;
}
/**
2022-03-10 11:54:29 +00:00
* Gets the placeholder formatter for a given name (including the delimiter char like %).
2018-02-01 20:01:12 +00:00
*
2022-03-10 11:54:29 +00:00
* @return callable|null
2018-02-01 20:01:12 +00:00
*/
2022-03-10 11:54:29 +00:00
public static function getPlaceholderFormatterDefinition(string $name)
2018-02-01 20:01:12 +00:00
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
}
2022-03-10 11:54:29 +00:00
return self::$formatters[$name] ?? null;
2018-02-01 20:01:12 +00:00
}
private function display()
{
if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
return;
}
2022-03-10 11:54:29 +00:00
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) {
if ($formatter = self::getPlaceholderFormatterDefinition($matches[1])) {
return $formatter($this);
2018-02-01 20:01:12 +00:00
}
return $matches[0];
2022-03-10 11:54:29 +00:00
}, $this->format ?? ''));
2018-02-01 20:01:12 +00:00
}
2022-03-10 11:54:29 +00:00
private function determineBestFormat(): string
2018-02-01 20:01:12 +00:00
{
switch ($this->output->getVerbosity()) {
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
case OutputInterface::VERBOSITY_VERBOSE:
return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi';
case OutputInterface::VERBOSITY_VERY_VERBOSE:
case OutputInterface::VERBOSITY_DEBUG:
return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi';
default:
return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
}
}
/**
* Overwrites a previous message to the output.
*/
private function overwrite(string $message)
2018-02-01 20:01:12 +00:00
{
if ($this->output->isDecorated()) {
$this->output->write("\x0D\x1B[2K");
$this->output->write($message);
} else {
$this->output->writeln($message);
}
}
2022-03-10 11:54:29 +00:00
private function getCurrentTimeInMilliseconds(): float
2018-02-01 20:01:12 +00:00
{
return round(microtime(true) * 1000);
}
2022-03-10 11:54:29 +00:00
private static function initPlaceholderFormatters(): array
2018-02-01 20:01:12 +00:00
{
2019-06-11 11:29:32 +00:00
return [
'indicator' => function (self $indicator) {
return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
2018-02-01 20:01:12 +00:00
},
2019-06-11 11:29:32 +00:00
'message' => function (self $indicator) {
2018-02-01 20:01:12 +00:00
return $indicator->message;
},
2019-06-11 11:29:32 +00:00
'elapsed' => function (self $indicator) {
2018-02-01 20:01:12 +00:00
return Helper::formatTime(time() - $indicator->startTime);
},
'memory' => function () {
return Helper::formatMemory(memory_get_usage(true));
},
2019-06-11 11:29:32 +00:00
];
2018-02-01 20:01:12 +00:00
}
}