ProgressIndicator.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWP_SCOPED\Symfony\Component\Console\Helper; |
| 12 | |
| 13 | use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 14 | use IAWP_SCOPED\Symfony\Component\Console\Exception\LogicException; |
| 15 | use IAWP_SCOPED\Symfony\Component\Console\Output\OutputInterface; |
| 16 | /** |
| 17 | * @author Kevin Bond <kevinbond@gmail.com> |
| 18 | */ |
| 19 | class ProgressIndicator |
| 20 | { |
| 21 | 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%)']; |
| 22 | private $output; |
| 23 | private $startTime; |
| 24 | private $format; |
| 25 | private $message; |
| 26 | private $indicatorValues; |
| 27 | private $indicatorCurrent; |
| 28 | private $indicatorChangeInterval; |
| 29 | private $indicatorUpdateTime; |
| 30 | private $started = \false; |
| 31 | /** |
| 32 | * @var array<string, callable> |
| 33 | */ |
| 34 | private static $formatters; |
| 35 | /** |
| 36 | * @param int $indicatorChangeInterval Change interval in milliseconds |
| 37 | * @param array|null $indicatorValues Animated indicator characters |
| 38 | */ |
| 39 | public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null) |
| 40 | { |
| 41 | $this->output = $output; |
| 42 | if (null === $format) { |
| 43 | $format = $this->determineBestFormat(); |
| 44 | } |
| 45 | if (null === $indicatorValues) { |
| 46 | $indicatorValues = ['-', '\\', '|', '/']; |
| 47 | } |
| 48 | $indicatorValues = \array_values($indicatorValues); |
| 49 | if (2 > \count($indicatorValues)) { |
| 50 | throw new InvalidArgumentException('Must have at least 2 indicator value characters.'); |
| 51 | } |
| 52 | $this->format = self::getFormatDefinition($format); |
| 53 | $this->indicatorChangeInterval = $indicatorChangeInterval; |
| 54 | $this->indicatorValues = $indicatorValues; |
| 55 | $this->startTime = \time(); |
| 56 | } |
| 57 | /** |
| 58 | * Sets the current indicator message. |
| 59 | */ |
| 60 | public function setMessage(?string $message) |
| 61 | { |
| 62 | $this->message = $message; |
| 63 | $this->display(); |
| 64 | } |
| 65 | /** |
| 66 | * Starts the indicator output. |
| 67 | */ |
| 68 | public function start(string $message) |
| 69 | { |
| 70 | if ($this->started) { |
| 71 | throw new LogicException('Progress indicator already started.'); |
| 72 | } |
| 73 | $this->message = $message; |
| 74 | $this->started = \true; |
| 75 | $this->startTime = \time(); |
| 76 | $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval; |
| 77 | $this->indicatorCurrent = 0; |
| 78 | $this->display(); |
| 79 | } |
| 80 | /** |
| 81 | * Advances the indicator. |
| 82 | */ |
| 83 | public function advance() |
| 84 | { |
| 85 | if (!$this->started) { |
| 86 | throw new LogicException('Progress indicator has not yet been started.'); |
| 87 | } |
| 88 | if (!$this->output->isDecorated()) { |
| 89 | return; |
| 90 | } |
| 91 | $currentTime = $this->getCurrentTimeInMilliseconds(); |
| 92 | if ($currentTime < $this->indicatorUpdateTime) { |
| 93 | return; |
| 94 | } |
| 95 | $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval; |
| 96 | ++$this->indicatorCurrent; |
| 97 | $this->display(); |
| 98 | } |
| 99 | /** |
| 100 | * Finish the indicator with message. |
| 101 | * |
| 102 | * @param $message |
| 103 | */ |
| 104 | public function finish(string $message) |
| 105 | { |
| 106 | if (!$this->started) { |
| 107 | throw new LogicException('Progress indicator has not yet been started.'); |
| 108 | } |
| 109 | $this->message = $message; |
| 110 | $this->display(); |
| 111 | $this->output->writeln(''); |
| 112 | $this->started = \false; |
| 113 | } |
| 114 | /** |
| 115 | * Gets the format for a given name. |
| 116 | * |
| 117 | * @return string|null |
| 118 | */ |
| 119 | public static function getFormatDefinition(string $name) |
| 120 | { |
| 121 | return self::FORMATS[$name] ?? null; |
| 122 | } |
| 123 | /** |
| 124 | * Sets a placeholder formatter for a given name. |
| 125 | * |
| 126 | * This method also allow you to override an existing placeholder. |
| 127 | */ |
| 128 | public static function setPlaceholderFormatterDefinition(string $name, callable $callable) |
| 129 | { |
| 130 | if (!self::$formatters) { |
| 131 | self::$formatters = self::initPlaceholderFormatters(); |
| 132 | } |
| 133 | self::$formatters[$name] = $callable; |
| 134 | } |
| 135 | /** |
| 136 | * Gets the placeholder formatter for a given name (including the delimiter char like %). |
| 137 | * |
| 138 | * @return callable|null |
| 139 | */ |
| 140 | public static function getPlaceholderFormatterDefinition(string $name) |
| 141 | { |
| 142 | if (!self::$formatters) { |
| 143 | self::$formatters = self::initPlaceholderFormatters(); |
| 144 | } |
| 145 | return self::$formatters[$name] ?? null; |
| 146 | } |
| 147 | private function display() |
| 148 | { |
| 149 | if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) { |
| 150 | return; |
| 151 | } |
| 152 | $this->overwrite(\preg_replace_callback("{%([a-z\\-_]+)(?:\\:([^%]+))?%}i", function ($matches) { |
| 153 | if ($formatter = self::getPlaceholderFormatterDefinition($matches[1])) { |
| 154 | return $formatter($this); |
| 155 | } |
| 156 | return $matches[0]; |
| 157 | }, $this->format ?? '')); |
| 158 | } |
| 159 | private function determineBestFormat() : string |
| 160 | { |
| 161 | switch ($this->output->getVerbosity()) { |
| 162 | // OutputInterface::VERBOSITY_QUIET: display is disabled anyway |
| 163 | case OutputInterface::VERBOSITY_VERBOSE: |
| 164 | return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi'; |
| 165 | case OutputInterface::VERBOSITY_VERY_VERBOSE: |
| 166 | case OutputInterface::VERBOSITY_DEBUG: |
| 167 | return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi'; |
| 168 | default: |
| 169 | return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi'; |
| 170 | } |
| 171 | } |
| 172 | /** |
| 173 | * Overwrites a previous message to the output. |
| 174 | */ |
| 175 | private function overwrite(string $message) |
| 176 | { |
| 177 | if ($this->output->isDecorated()) { |
| 178 | $this->output->write("\r\x1b[2K"); |
| 179 | $this->output->write($message); |
| 180 | } else { |
| 181 | $this->output->writeln($message); |
| 182 | } |
| 183 | } |
| 184 | private function getCurrentTimeInMilliseconds() : float |
| 185 | { |
| 186 | return \round(\microtime(\true) * 1000); |
| 187 | } |
| 188 | private static function initPlaceholderFormatters() : array |
| 189 | { |
| 190 | return ['indicator' => function (self $indicator) { |
| 191 | return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)]; |
| 192 | }, 'message' => function (self $indicator) { |
| 193 | return $indicator->message; |
| 194 | }, 'elapsed' => function (self $indicator) { |
| 195 | return Helper::formatTime(\time() - $indicator->startTime); |
| 196 | }, 'memory' => function () { |
| 197 | return Helper::formatMemory(\memory_get_usage(\true)); |
| 198 | }]; |
| 199 | } |
| 200 | } |
| 201 |