PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.28.0
Independent Analytics – WordPress Analytics Plugin v1.28.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / console / Helper / ProgressBar.php
independent-analytics / vendor / symfony / console / Helper Last commit date
DebugFormatterHelper.php 3 years ago DescriptorHelper.php 3 years ago Dumper.php 3 years ago FormatterHelper.php 3 years ago Helper.php 3 years ago HelperInterface.php 3 years ago HelperSet.php 3 years ago InputAwareHelper.php 3 years ago ProcessHelper.php 3 years ago ProgressBar.php 3 years ago ProgressIndicator.php 3 years ago QuestionHelper.php 3 years ago SymfonyQuestionHelper.php 3 years ago Table.php 3 years ago TableCell.php 3 years ago TableCellStyle.php 3 years ago TableRows.php 3 years ago TableSeparator.php 3 years ago TableStyle.php 3 years ago
ProgressBar.php
500 lines
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\Cursor;
14 use IAWP_SCOPED\Symfony\Component\Console\Exception\LogicException;
15 use IAWP_SCOPED\Symfony\Component\Console\Output\ConsoleOutputInterface;
16 use IAWP_SCOPED\Symfony\Component\Console\Output\ConsoleSectionOutput;
17 use IAWP_SCOPED\Symfony\Component\Console\Output\OutputInterface;
18 use IAWP_SCOPED\Symfony\Component\Console\Terminal;
19 /**
20 * The ProgressBar provides helpers to display progress output.
21 *
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Chris Jones <leeked@gmail.com>
24 */
25 final class ProgressBar
26 {
27 public const FORMAT_VERBOSE = 'verbose';
28 public const FORMAT_VERY_VERBOSE = 'very_verbose';
29 public const FORMAT_DEBUG = 'debug';
30 public const FORMAT_NORMAL = 'normal';
31 private const FORMAT_VERBOSE_NOMAX = 'verbose_nomax';
32 private const FORMAT_VERY_VERBOSE_NOMAX = 'very_verbose_nomax';
33 private const FORMAT_DEBUG_NOMAX = 'debug_nomax';
34 private const FORMAT_NORMAL_NOMAX = 'normal_nomax';
35 private $barWidth = 28;
36 private $barChar;
37 private $emptyBarChar = '-';
38 private $progressChar = '>';
39 private $format;
40 private $internalFormat;
41 private $redrawFreq = 1;
42 private $writeCount;
43 private $lastWriteTime;
44 private $minSecondsBetweenRedraws = 0;
45 private $maxSecondsBetweenRedraws = 1;
46 private $output;
47 private $step = 0;
48 private $max;
49 private $startTime;
50 private $stepWidth;
51 private $percent = 0.0;
52 private $messages = [];
53 private $overwrite = \true;
54 private $terminal;
55 private $previousMessage;
56 private $cursor;
57 private static $formatters;
58 private static $formats;
59 /**
60 * @param int $max Maximum steps (0 if unknown)
61 */
62 public function __construct(OutputInterface $output, int $max = 0, float $minSecondsBetweenRedraws = 1 / 25)
63 {
64 if ($output instanceof ConsoleOutputInterface) {
65 $output = $output->getErrorOutput();
66 }
67 $this->output = $output;
68 $this->setMaxSteps($max);
69 $this->terminal = new Terminal();
70 if (0 < $minSecondsBetweenRedraws) {
71 $this->redrawFreq = null;
72 $this->minSecondsBetweenRedraws = $minSecondsBetweenRedraws;
73 }
74 if (!$this->output->isDecorated()) {
75 // disable overwrite when output does not support ANSI codes.
76 $this->overwrite = \false;
77 // set a reasonable redraw frequency so output isn't flooded
78 $this->redrawFreq = null;
79 }
80 $this->startTime = \time();
81 $this->cursor = new Cursor($output);
82 }
83 /**
84 * Sets a placeholder formatter for a given name.
85 *
86 * This method also allow you to override an existing placeholder.
87 *
88 * @param string $name The placeholder name (including the delimiter char like %)
89 * @param callable $callable A PHP callable
90 */
91 public static function setPlaceholderFormatterDefinition(string $name, callable $callable) : void
92 {
93 if (!self::$formatters) {
94 self::$formatters = self::initPlaceholderFormatters();
95 }
96 self::$formatters[$name] = $callable;
97 }
98 /**
99 * Gets the placeholder formatter for a given name.
100 *
101 * @param string $name The placeholder name (including the delimiter char like %)
102 */
103 public static function getPlaceholderFormatterDefinition(string $name) : ?callable
104 {
105 if (!self::$formatters) {
106 self::$formatters = self::initPlaceholderFormatters();
107 }
108 return self::$formatters[$name] ?? null;
109 }
110 /**
111 * Sets a format for a given name.
112 *
113 * This method also allow you to override an existing format.
114 *
115 * @param string $name The format name
116 * @param string $format A format string
117 */
118 public static function setFormatDefinition(string $name, string $format) : void
119 {
120 if (!self::$formats) {
121 self::$formats = self::initFormats();
122 }
123 self::$formats[$name] = $format;
124 }
125 /**
126 * Gets the format for a given name.
127 *
128 * @param string $name The format name
129 */
130 public static function getFormatDefinition(string $name) : ?string
131 {
132 if (!self::$formats) {
133 self::$formats = self::initFormats();
134 }
135 return self::$formats[$name] ?? null;
136 }
137 /**
138 * Associates a text with a named placeholder.
139 *
140 * The text is displayed when the progress bar is rendered but only
141 * when the corresponding placeholder is part of the custom format line
142 * (by wrapping the name with %).
143 *
144 * @param string $message The text to associate with the placeholder
145 * @param string $name The name of the placeholder
146 */
147 public function setMessage(string $message, string $name = 'message')
148 {
149 $this->messages[$name] = $message;
150 }
151 public function getMessage(string $name = 'message')
152 {
153 return $this->messages[$name];
154 }
155 public function getStartTime() : int
156 {
157 return $this->startTime;
158 }
159 public function getMaxSteps() : int
160 {
161 return $this->max;
162 }
163 public function getProgress() : int
164 {
165 return $this->step;
166 }
167 private function getStepWidth() : int
168 {
169 return $this->stepWidth;
170 }
171 public function getProgressPercent() : float
172 {
173 return $this->percent;
174 }
175 public function getBarOffset() : float
176 {
177 return \floor($this->max ? $this->percent * $this->barWidth : (null === $this->redrawFreq ? (int) (\min(5, $this->barWidth / 15) * $this->writeCount) : $this->step) % $this->barWidth);
178 }
179 public function getEstimated() : float
180 {
181 if (!$this->step) {
182 return 0;
183 }
184 return \round((\time() - $this->startTime) / $this->step * $this->max);
185 }
186 public function getRemaining() : float
187 {
188 if (!$this->step) {
189 return 0;
190 }
191 return \round((\time() - $this->startTime) / $this->step * ($this->max - $this->step));
192 }
193 public function setBarWidth(int $size)
194 {
195 $this->barWidth = \max(1, $size);
196 }
197 public function getBarWidth() : int
198 {
199 return $this->barWidth;
200 }
201 public function setBarCharacter(string $char)
202 {
203 $this->barChar = $char;
204 }
205 public function getBarCharacter() : string
206 {
207 return $this->barChar ?? ($this->max ? '=' : $this->emptyBarChar);
208 }
209 public function setEmptyBarCharacter(string $char)
210 {
211 $this->emptyBarChar = $char;
212 }
213 public function getEmptyBarCharacter() : string
214 {
215 return $this->emptyBarChar;
216 }
217 public function setProgressCharacter(string $char)
218 {
219 $this->progressChar = $char;
220 }
221 public function getProgressCharacter() : string
222 {
223 return $this->progressChar;
224 }
225 public function setFormat(string $format)
226 {
227 $this->format = null;
228 $this->internalFormat = $format;
229 }
230 /**
231 * Sets the redraw frequency.
232 *
233 * @param int|null $freq The frequency in steps
234 */
235 public function setRedrawFrequency(?int $freq)
236 {
237 $this->redrawFreq = null !== $freq ? \max(1, $freq) : null;
238 }
239 public function minSecondsBetweenRedraws(float $seconds) : void
240 {
241 $this->minSecondsBetweenRedraws = $seconds;
242 }
243 public function maxSecondsBetweenRedraws(float $seconds) : void
244 {
245 $this->maxSecondsBetweenRedraws = $seconds;
246 }
247 /**
248 * Returns an iterator that will automatically update the progress bar when iterated.
249 *
250 * @param int|null $max Number of steps to complete the bar (0 if indeterminate), if null it will be inferred from $iterable
251 */
252 public function iterate(iterable $iterable, int $max = null) : iterable
253 {
254 $this->start($max ?? (\is_countable($iterable) ? \count($iterable) : 0));
255 foreach ($iterable as $key => $value) {
256 (yield $key => $value);
257 $this->advance();
258 }
259 $this->finish();
260 }
261 /**
262 * Starts the progress output.
263 *
264 * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged
265 */
266 public function start(int $max = null)
267 {
268 $this->startTime = \time();
269 $this->step = 0;
270 $this->percent = 0.0;
271 if (null !== $max) {
272 $this->setMaxSteps($max);
273 }
274 $this->display();
275 }
276 /**
277 * Advances the progress output X steps.
278 *
279 * @param int $step Number of steps to advance
280 */
281 public function advance(int $step = 1)
282 {
283 $this->setProgress($this->step + $step);
284 }
285 /**
286 * Sets whether to overwrite the progressbar, false for new line.
287 */
288 public function setOverwrite(bool $overwrite)
289 {
290 $this->overwrite = $overwrite;
291 }
292 public function setProgress(int $step)
293 {
294 if ($this->max && $step > $this->max) {
295 $this->max = $step;
296 } elseif ($step < 0) {
297 $step = 0;
298 }
299 $redrawFreq = $this->redrawFreq ?? ($this->max ?: 10) / 10;
300 $prevPeriod = (int) ($this->step / $redrawFreq);
301 $currPeriod = (int) ($step / $redrawFreq);
302 $this->step = $step;
303 $this->percent = $this->max ? (float) $this->step / $this->max : 0;
304 $timeInterval = \microtime(\true) - $this->lastWriteTime;
305 // Draw regardless of other limits
306 if ($this->max === $step) {
307 $this->display();
308 return;
309 }
310 // Throttling
311 if ($timeInterval < $this->minSecondsBetweenRedraws) {
312 return;
313 }
314 // Draw each step period, but not too late
315 if ($prevPeriod !== $currPeriod || $timeInterval >= $this->maxSecondsBetweenRedraws) {
316 $this->display();
317 }
318 }
319 public function setMaxSteps(int $max)
320 {
321 $this->format = null;
322 $this->max = \max(0, $max);
323 $this->stepWidth = $this->max ? Helper::width((string) $this->max) : 4;
324 }
325 /**
326 * Finishes the progress output.
327 */
328 public function finish() : void
329 {
330 if (!$this->max) {
331 $this->max = $this->step;
332 }
333 if ($this->step === $this->max && !$this->overwrite) {
334 // prevent double 100% output
335 return;
336 }
337 $this->setProgress($this->max);
338 }
339 /**
340 * Outputs the current progress string.
341 */
342 public function display() : void
343 {
344 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
345 return;
346 }
347 if (null === $this->format) {
348 $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
349 }
350 $this->overwrite($this->buildLine());
351 }
352 /**
353 * Removes the progress bar from the current line.
354 *
355 * This is useful if you wish to write some output
356 * while a progress bar is running.
357 * Call display() to show the progress bar again.
358 */
359 public function clear() : void
360 {
361 if (!$this->overwrite) {
362 return;
363 }
364 if (null === $this->format) {
365 $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
366 }
367 $this->overwrite('');
368 }
369 private function setRealFormat(string $format)
370 {
371 // try to use the _nomax variant if available
372 if (!$this->max && null !== self::getFormatDefinition($format . '_nomax')) {
373 $this->format = self::getFormatDefinition($format . '_nomax');
374 } elseif (null !== self::getFormatDefinition($format)) {
375 $this->format = self::getFormatDefinition($format);
376 } else {
377 $this->format = $format;
378 }
379 }
380 /**
381 * Overwrites a previous message to the output.
382 */
383 private function overwrite(string $message) : void
384 {
385 if ($this->previousMessage === $message) {
386 return;
387 }
388 $originalMessage = $message;
389 if ($this->overwrite) {
390 if (null !== $this->previousMessage) {
391 if ($this->output instanceof ConsoleSectionOutput) {
392 $messageLines = \explode("\n", $this->previousMessage);
393 $lineCount = \count($messageLines);
394 foreach ($messageLines as $messageLine) {
395 $messageLineLength = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $messageLine));
396 if ($messageLineLength > $this->terminal->getWidth()) {
397 $lineCount += \floor($messageLineLength / $this->terminal->getWidth());
398 }
399 }
400 $this->output->clear($lineCount);
401 } else {
402 $lineCount = \substr_count($this->previousMessage, "\n");
403 for ($i = 0; $i < $lineCount; ++$i) {
404 $this->cursor->moveToColumn(1);
405 $this->cursor->clearLine();
406 $this->cursor->moveUp();
407 }
408 $this->cursor->moveToColumn(1);
409 $this->cursor->clearLine();
410 }
411 }
412 } elseif ($this->step > 0) {
413 $message = \PHP_EOL . $message;
414 }
415 $this->previousMessage = $originalMessage;
416 $this->lastWriteTime = \microtime(\true);
417 $this->output->write($message);
418 ++$this->writeCount;
419 }
420 private function determineBestFormat() : string
421 {
422 switch ($this->output->getVerbosity()) {
423 // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
424 case OutputInterface::VERBOSITY_VERBOSE:
425 return $this->max ? self::FORMAT_VERBOSE : self::FORMAT_VERBOSE_NOMAX;
426 case OutputInterface::VERBOSITY_VERY_VERBOSE:
427 return $this->max ? self::FORMAT_VERY_VERBOSE : self::FORMAT_VERY_VERBOSE_NOMAX;
428 case OutputInterface::VERBOSITY_DEBUG:
429 return $this->max ? self::FORMAT_DEBUG : self::FORMAT_DEBUG_NOMAX;
430 default:
431 return $this->max ? self::FORMAT_NORMAL : self::FORMAT_NORMAL_NOMAX;
432 }
433 }
434 private static function initPlaceholderFormatters() : array
435 {
436 return ['bar' => function (self $bar, OutputInterface $output) {
437 $completeBars = $bar->getBarOffset();
438 $display = \str_repeat($bar->getBarCharacter(), $completeBars);
439 if ($completeBars < $bar->getBarWidth()) {
440 $emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter()));
441 $display .= $bar->getProgressCharacter() . \str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
442 }
443 return $display;
444 }, 'elapsed' => function (self $bar) {
445 return Helper::formatTime(\time() - $bar->getStartTime());
446 }, 'remaining' => function (self $bar) {
447 if (!$bar->getMaxSteps()) {
448 throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
449 }
450 return Helper::formatTime($bar->getRemaining());
451 }, 'estimated' => function (self $bar) {
452 if (!$bar->getMaxSteps()) {
453 throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
454 }
455 return Helper::formatTime($bar->getEstimated());
456 }, 'memory' => function (self $bar) {
457 return Helper::formatMemory(\memory_get_usage(\true));
458 }, 'current' => function (self $bar) {
459 return \str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', \STR_PAD_LEFT);
460 }, 'max' => function (self $bar) {
461 return $bar->getMaxSteps();
462 }, 'percent' => function (self $bar) {
463 return \floor($bar->getProgressPercent() * 100);
464 }];
465 }
466 private static function initFormats() : array
467 {
468 return [self::FORMAT_NORMAL => ' %current%/%max% [%bar%] %percent:3s%%', self::FORMAT_NORMAL_NOMAX => ' %current% [%bar%]', self::FORMAT_VERBOSE => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%', self::FORMAT_VERBOSE_NOMAX => ' %current% [%bar%] %elapsed:6s%', self::FORMAT_VERY_VERBOSE => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%', self::FORMAT_VERY_VERBOSE_NOMAX => ' %current% [%bar%] %elapsed:6s%', self::FORMAT_DEBUG => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%', self::FORMAT_DEBUG_NOMAX => ' %current% [%bar%] %elapsed:6s% %memory:6s%'];
469 }
470 private function buildLine() : string
471 {
472 $regex = "{%([a-z\\-_]+)(?:\\:([^%]+))?%}i";
473 $callback = function ($matches) {
474 if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
475 $text = $formatter($this, $this->output);
476 } elseif (isset($this->messages[$matches[1]])) {
477 $text = $this->messages[$matches[1]];
478 } else {
479 return $matches[0];
480 }
481 if (isset($matches[2])) {
482 $text = \sprintf('%' . $matches[2], $text);
483 }
484 return $text;
485 };
486 $line = \preg_replace_callback($regex, $callback, $this->format);
487 // gets string length for each sub line with multiline format
488 $linesLength = \array_map(function ($subLine) {
489 return Helper::width(Helper::removeDecoration($this->output->getFormatter(), \rtrim($subLine, "\r")));
490 }, \explode("\n", $line));
491 $linesWidth = \max($linesLength);
492 $terminalWidth = $this->terminal->getWidth();
493 if ($linesWidth <= $terminalWidth) {
494 return $line;
495 }
496 $this->setBarWidth($this->barWidth - $linesWidth + $terminalWidth);
497 return \preg_replace_callback($regex, $callback, $this->format);
498 }
499 }
500