annotate vendor/symfony/console/Helper/ProgressIndicator.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Helper;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 15 use Symfony\Component\Console\Exception\LogicException;
Chris@0 16 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * @author Kevin Bond <kevinbond@gmail.com>
Chris@0 20 */
Chris@0 21 class ProgressIndicator
Chris@0 22 {
Chris@0 23 private $output;
Chris@0 24 private $startTime;
Chris@0 25 private $format;
Chris@0 26 private $message;
Chris@0 27 private $indicatorValues;
Chris@0 28 private $indicatorCurrent;
Chris@0 29 private $indicatorChangeInterval;
Chris@0 30 private $indicatorUpdateTime;
Chris@0 31 private $started = false;
Chris@0 32
Chris@0 33 private static $formatters;
Chris@0 34 private static $formats;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @param OutputInterface $output
Chris@0 38 * @param string|null $format Indicator format
Chris@0 39 * @param int $indicatorChangeInterval Change interval in milliseconds
Chris@0 40 * @param array|null $indicatorValues Animated indicator characters
Chris@0 41 */
Chris@0 42 public function __construct(OutputInterface $output, $format = null, $indicatorChangeInterval = 100, $indicatorValues = null)
Chris@0 43 {
Chris@0 44 $this->output = $output;
Chris@0 45
Chris@0 46 if (null === $format) {
Chris@0 47 $format = $this->determineBestFormat();
Chris@0 48 }
Chris@0 49
Chris@0 50 if (null === $indicatorValues) {
Chris@17 51 $indicatorValues = ['-', '\\', '|', '/'];
Chris@0 52 }
Chris@0 53
Chris@0 54 $indicatorValues = array_values($indicatorValues);
Chris@0 55
Chris@17 56 if (2 > \count($indicatorValues)) {
Chris@0 57 throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
Chris@0 58 }
Chris@0 59
Chris@0 60 $this->format = self::getFormatDefinition($format);
Chris@0 61 $this->indicatorChangeInterval = $indicatorChangeInterval;
Chris@0 62 $this->indicatorValues = $indicatorValues;
Chris@0 63 $this->startTime = time();
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Sets the current indicator message.
Chris@0 68 *
Chris@0 69 * @param string|null $message
Chris@0 70 */
Chris@0 71 public function setMessage($message)
Chris@0 72 {
Chris@0 73 $this->message = $message;
Chris@0 74
Chris@0 75 $this->display();
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Starts the indicator output.
Chris@0 80 *
Chris@0 81 * @param $message
Chris@0 82 */
Chris@0 83 public function start($message)
Chris@0 84 {
Chris@0 85 if ($this->started) {
Chris@0 86 throw new LogicException('Progress indicator already started.');
Chris@0 87 }
Chris@0 88
Chris@0 89 $this->message = $message;
Chris@0 90 $this->started = true;
Chris@0 91 $this->startTime = time();
Chris@0 92 $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
Chris@0 93 $this->indicatorCurrent = 0;
Chris@0 94
Chris@0 95 $this->display();
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Advances the indicator.
Chris@0 100 */
Chris@0 101 public function advance()
Chris@0 102 {
Chris@0 103 if (!$this->started) {
Chris@0 104 throw new LogicException('Progress indicator has not yet been started.');
Chris@0 105 }
Chris@0 106
Chris@0 107 if (!$this->output->isDecorated()) {
Chris@0 108 return;
Chris@0 109 }
Chris@0 110
Chris@0 111 $currentTime = $this->getCurrentTimeInMilliseconds();
Chris@0 112
Chris@0 113 if ($currentTime < $this->indicatorUpdateTime) {
Chris@0 114 return;
Chris@0 115 }
Chris@0 116
Chris@0 117 $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
Chris@0 118 ++$this->indicatorCurrent;
Chris@0 119
Chris@0 120 $this->display();
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Finish the indicator with message.
Chris@0 125 *
Chris@0 126 * @param $message
Chris@0 127 */
Chris@0 128 public function finish($message)
Chris@0 129 {
Chris@0 130 if (!$this->started) {
Chris@0 131 throw new LogicException('Progress indicator has not yet been started.');
Chris@0 132 }
Chris@0 133
Chris@0 134 $this->message = $message;
Chris@0 135 $this->display();
Chris@0 136 $this->output->writeln('');
Chris@0 137 $this->started = false;
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Gets the format for a given name.
Chris@0 142 *
Chris@0 143 * @param string $name The format name
Chris@0 144 *
Chris@0 145 * @return string|null A format string
Chris@0 146 */
Chris@0 147 public static function getFormatDefinition($name)
Chris@0 148 {
Chris@0 149 if (!self::$formats) {
Chris@0 150 self::$formats = self::initFormats();
Chris@0 151 }
Chris@0 152
Chris@0 153 return isset(self::$formats[$name]) ? self::$formats[$name] : null;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Sets a placeholder formatter for a given name.
Chris@0 158 *
Chris@0 159 * This method also allow you to override an existing placeholder.
Chris@0 160 *
Chris@0 161 * @param string $name The placeholder name (including the delimiter char like %)
Chris@0 162 * @param callable $callable A PHP callable
Chris@0 163 */
Chris@0 164 public static function setPlaceholderFormatterDefinition($name, $callable)
Chris@0 165 {
Chris@0 166 if (!self::$formatters) {
Chris@0 167 self::$formatters = self::initPlaceholderFormatters();
Chris@0 168 }
Chris@0 169
Chris@0 170 self::$formatters[$name] = $callable;
Chris@0 171 }
Chris@0 172
Chris@0 173 /**
Chris@0 174 * Gets the placeholder formatter for a given name.
Chris@0 175 *
Chris@0 176 * @param string $name The placeholder name (including the delimiter char like %)
Chris@0 177 *
Chris@0 178 * @return callable|null A PHP callable
Chris@0 179 */
Chris@0 180 public static function getPlaceholderFormatterDefinition($name)
Chris@0 181 {
Chris@0 182 if (!self::$formatters) {
Chris@0 183 self::$formatters = self::initPlaceholderFormatters();
Chris@0 184 }
Chris@0 185
Chris@0 186 return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
Chris@0 187 }
Chris@0 188
Chris@0 189 private function display()
Chris@0 190 {
Chris@0 191 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
Chris@0 192 return;
Chris@0 193 }
Chris@0 194
Chris@0 195 $self = $this;
Chris@0 196
Chris@0 197 $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
Chris@0 198 if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
Chris@17 199 return \call_user_func($formatter, $self);
Chris@0 200 }
Chris@0 201
Chris@0 202 return $matches[0];
Chris@0 203 }, $this->format));
Chris@0 204 }
Chris@0 205
Chris@0 206 private function determineBestFormat()
Chris@0 207 {
Chris@0 208 switch ($this->output->getVerbosity()) {
Chris@0 209 // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
Chris@0 210 case OutputInterface::VERBOSITY_VERBOSE:
Chris@0 211 return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi';
Chris@0 212 case OutputInterface::VERBOSITY_VERY_VERBOSE:
Chris@0 213 case OutputInterface::VERBOSITY_DEBUG:
Chris@0 214 return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi';
Chris@0 215 default:
Chris@0 216 return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
Chris@0 217 }
Chris@0 218 }
Chris@0 219
Chris@0 220 /**
Chris@0 221 * Overwrites a previous message to the output.
Chris@0 222 *
Chris@0 223 * @param string $message The message
Chris@0 224 */
Chris@0 225 private function overwrite($message)
Chris@0 226 {
Chris@0 227 if ($this->output->isDecorated()) {
Chris@0 228 $this->output->write("\x0D\x1B[2K");
Chris@0 229 $this->output->write($message);
Chris@0 230 } else {
Chris@0 231 $this->output->writeln($message);
Chris@0 232 }
Chris@0 233 }
Chris@0 234
Chris@0 235 private function getCurrentTimeInMilliseconds()
Chris@0 236 {
Chris@0 237 return round(microtime(true) * 1000);
Chris@0 238 }
Chris@0 239
Chris@0 240 private static function initPlaceholderFormatters()
Chris@0 241 {
Chris@17 242 return [
Chris@17 243 'indicator' => function (self $indicator) {
Chris@17 244 return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
Chris@0 245 },
Chris@17 246 'message' => function (self $indicator) {
Chris@0 247 return $indicator->message;
Chris@0 248 },
Chris@17 249 'elapsed' => function (self $indicator) {
Chris@0 250 return Helper::formatTime(time() - $indicator->startTime);
Chris@0 251 },
Chris@0 252 'memory' => function () {
Chris@0 253 return Helper::formatMemory(memory_get_usage(true));
Chris@0 254 },
Chris@17 255 ];
Chris@0 256 }
Chris@0 257
Chris@0 258 private static function initFormats()
Chris@0 259 {
Chris@17 260 return [
Chris@0 261 'normal' => ' %indicator% %message%',
Chris@0 262 'normal_no_ansi' => ' %message%',
Chris@0 263
Chris@0 264 'verbose' => ' %indicator% %message% (%elapsed:6s%)',
Chris@0 265 'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
Chris@0 266
Chris@0 267 'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
Chris@0 268 'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
Chris@17 269 ];
Chris@0 270 }
Chris@0 271 }