1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 19:42:38 +02:00
Daniel Neto 2024-03-05 19:22:51 -03:00
parent 7673eda07e
commit 330cdbe615
9054 changed files with 480487 additions and 41800 deletions

View file

@ -31,37 +31,31 @@ class ProgressIndicator
'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
];
private $output;
private $startTime;
private $format;
private $message;
private $indicatorValues;
private $indicatorCurrent;
private $indicatorChangeInterval;
private $indicatorUpdateTime;
private $started = false;
private OutputInterface $output;
private int $startTime;
private ?string $format = null;
private ?string $message = null;
private array $indicatorValues;
private int $indicatorCurrent;
private int $indicatorChangeInterval;
private float $indicatorUpdateTime;
private bool $started = false;
/**
* @var array<string, callable>
*/
private static $formatters;
private static array $formatters;
/**
* @param int $indicatorChangeInterval Change interval in milliseconds
* @param array|null $indicatorValues Animated indicator characters
*/
public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null)
public function __construct(OutputInterface $output, ?string $format = null, int $indicatorChangeInterval = 100, ?array $indicatorValues = null)
{
$this->output = $output;
if (null === $format) {
$format = $this->determineBestFormat();
}
if (null === $indicatorValues) {
$indicatorValues = ['-', '\\', '|', '/'];
}
$format ??= $this->determineBestFormat();
$indicatorValues ??= ['-', '\\', '|', '/'];
$indicatorValues = array_values($indicatorValues);
if (2 > \count($indicatorValues)) {
@ -76,6 +70,8 @@ class ProgressIndicator
/**
* Sets the current indicator message.
*
* @return void
*/
public function setMessage(?string $message)
{
@ -86,6 +82,8 @@ class ProgressIndicator
/**
* Starts the indicator output.
*
* @return void
*/
public function start(string $message)
{
@ -104,6 +102,8 @@ class ProgressIndicator
/**
* Advances the indicator.
*
* @return void
*/
public function advance()
{
@ -130,7 +130,7 @@ class ProgressIndicator
/**
* Finish the indicator with message.
*
* @param $message
* @return void
*/
public function finish(string $message)
{
@ -146,10 +146,8 @@ class ProgressIndicator
/**
* Gets the format for a given name.
*
* @return string|null
*/
public static function getFormatDefinition(string $name)
public static function getFormatDefinition(string $name): ?string
{
return self::FORMATS[$name] ?? null;
}
@ -158,31 +156,27 @@ class ProgressIndicator
* Sets a placeholder formatter for a given name.
*
* This method also allow you to override an existing placeholder.
*
* @return void
*/
public static function setPlaceholderFormatterDefinition(string $name, callable $callable)
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
}
self::$formatters ??= self::initPlaceholderFormatters();
self::$formatters[$name] = $callable;
}
/**
* Gets the placeholder formatter for a given name (including the delimiter char like %).
*
* @return callable|null
*/
public static function getPlaceholderFormatterDefinition(string $name)
public static function getPlaceholderFormatterDefinition(string $name): ?callable
{
if (!self::$formatters) {
self::$formatters = self::initPlaceholderFormatters();
}
self::$formatters ??= self::initPlaceholderFormatters();
return self::$formatters[$name] ?? null;
}
private function display()
private function display(): void
{
if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
return;
@ -199,22 +193,19 @@ class ProgressIndicator
private function determineBestFormat(): string
{
switch ($this->output->getVerbosity()) {
return match ($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';
}
OutputInterface::VERBOSITY_VERBOSE => $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi',
OutputInterface::VERBOSITY_VERY_VERBOSE,
OutputInterface::VERBOSITY_DEBUG => $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi',
default => $this->output->isDecorated() ? 'normal' : 'normal_no_ansi',
};
}
/**
* Overwrites a previous message to the output.
*/
private function overwrite(string $message)
private function overwrite(string $message): void
{
if ($this->output->isDecorated()) {
$this->output->write("\x0D\x1B[2K");
@ -229,21 +220,16 @@ class ProgressIndicator
return round(microtime(true) * 1000);
}
/**
* @return array<string, \Closure>
*/
private static function initPlaceholderFormatters(): array
{
return [
'indicator' => function (self $indicator) {
return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
},
'message' => function (self $indicator) {
return $indicator->message;
},
'elapsed' => function (self $indicator) {
return Helper::formatTime(time() - $indicator->startTime);
},
'memory' => function () {
return Helper::formatMemory(memory_get_usage(true));
},
'indicator' => fn (self $indicator) => $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)],
'message' => fn (self $indicator) => $indicator->message,
'elapsed' => fn (self $indicator) => Helper::formatTime(time() - $indicator->startTime, 2),
'memory' => fn () => Helper::formatMemory(memory_get_usage(true)),
];
}
}