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