Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / symfony / console / Helper / ProgressBar.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
12 namespace Symfony\Component\Console\Helper;
13
14 use Symfony\Component\Console\Exception\LogicException;
15 use Symfony\Component\Console\Output\ConsoleOutputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Terminal;
18
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     private $barWidth = 28;
28     private $barChar;
29     private $emptyBarChar = '-';
30     private $progressChar = '>';
31     private $format;
32     private $internalFormat;
33     private $redrawFreq = 1;
34     private $output;
35     private $step = 0;
36     private $max;
37     private $startTime;
38     private $stepWidth;
39     private $percent = 0.0;
40     private $formatLineCount;
41     private $messages = array();
42     private $overwrite = true;
43     private $terminal;
44     private $firstRun = true;
45
46     private static $formatters;
47     private static $formats;
48
49     /**
50      * @param OutputInterface $output An OutputInterface instance
51      * @param int             $max    Maximum steps (0 if unknown)
52      */
53     public function __construct(OutputInterface $output, $max = 0)
54     {
55         if ($output instanceof ConsoleOutputInterface) {
56             $output = $output->getErrorOutput();
57         }
58
59         $this->output = $output;
60         $this->setMaxSteps($max);
61         $this->terminal = new Terminal();
62
63         if (!$this->output->isDecorated()) {
64             // disable overwrite when output does not support ANSI codes.
65             $this->overwrite = false;
66
67             // set a reasonable redraw frequency so output isn't flooded
68             $this->setRedrawFrequency($max / 10);
69         }
70
71         $this->startTime = time();
72     }
73
74     /**
75      * Sets a placeholder formatter for a given name.
76      *
77      * This method also allow you to override an existing placeholder.
78      *
79      * @param string   $name     The placeholder name (including the delimiter char like %)
80      * @param callable $callable A PHP callable
81      */
82     public static function setPlaceholderFormatterDefinition($name, callable $callable)
83     {
84         if (!self::$formatters) {
85             self::$formatters = self::initPlaceholderFormatters();
86         }
87
88         self::$formatters[$name] = $callable;
89     }
90
91     /**
92      * Gets the placeholder formatter for a given name.
93      *
94      * @param string $name The placeholder name (including the delimiter char like %)
95      *
96      * @return callable|null A PHP callable
97      */
98     public static function getPlaceholderFormatterDefinition($name)
99     {
100         if (!self::$formatters) {
101             self::$formatters = self::initPlaceholderFormatters();
102         }
103
104         return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
105     }
106
107     /**
108      * Sets a format for a given name.
109      *
110      * This method also allow you to override an existing format.
111      *
112      * @param string $name   The format name
113      * @param string $format A format string
114      */
115     public static function setFormatDefinition($name, $format)
116     {
117         if (!self::$formats) {
118             self::$formats = self::initFormats();
119         }
120
121         self::$formats[$name] = $format;
122     }
123
124     /**
125      * Gets the format for a given name.
126      *
127      * @param string $name The format name
128      *
129      * @return string|null A format string
130      */
131     public static function getFormatDefinition($name)
132     {
133         if (!self::$formats) {
134             self::$formats = self::initFormats();
135         }
136
137         return isset(self::$formats[$name]) ? self::$formats[$name] : null;
138     }
139
140     /**
141      * Associates a text with a named placeholder.
142      *
143      * The text is displayed when the progress bar is rendered but only
144      * when the corresponding placeholder is part of the custom format line
145      * (by wrapping the name with %).
146      *
147      * @param string $message The text to associate with the placeholder
148      * @param string $name    The name of the placeholder
149      */
150     public function setMessage($message, $name = 'message')
151     {
152         $this->messages[$name] = $message;
153     }
154
155     public function getMessage($name = 'message')
156     {
157         return $this->messages[$name];
158     }
159
160     /**
161      * Gets the progress bar start time.
162      *
163      * @return int The progress bar start time
164      */
165     public function getStartTime()
166     {
167         return $this->startTime;
168     }
169
170     /**
171      * Gets the progress bar maximal steps.
172      *
173      * @return int The progress bar max steps
174      */
175     public function getMaxSteps()
176     {
177         return $this->max;
178     }
179
180     /**
181      * Gets the current step position.
182      *
183      * @return int The progress bar step
184      */
185     public function getProgress()
186     {
187         return $this->step;
188     }
189
190     /**
191      * Gets the progress bar step width.
192      *
193      * @return int The progress bar step width
194      */
195     private function getStepWidth()
196     {
197         return $this->stepWidth;
198     }
199
200     /**
201      * Gets the current progress bar percent.
202      *
203      * @return float The current progress bar percent
204      */
205     public function getProgressPercent()
206     {
207         return $this->percent;
208     }
209
210     /**
211      * Sets the progress bar width.
212      *
213      * @param int $size The progress bar size
214      */
215     public function setBarWidth($size)
216     {
217         $this->barWidth = max(1, (int) $size);
218     }
219
220     /**
221      * Gets the progress bar width.
222      *
223      * @return int The progress bar size
224      */
225     public function getBarWidth()
226     {
227         return $this->barWidth;
228     }
229
230     /**
231      * Sets the bar character.
232      *
233      * @param string $char A character
234      */
235     public function setBarCharacter($char)
236     {
237         $this->barChar = $char;
238     }
239
240     /**
241      * Gets the bar character.
242      *
243      * @return string A character
244      */
245     public function getBarCharacter()
246     {
247         if (null === $this->barChar) {
248             return $this->max ? '=' : $this->emptyBarChar;
249         }
250
251         return $this->barChar;
252     }
253
254     /**
255      * Sets the empty bar character.
256      *
257      * @param string $char A character
258      */
259     public function setEmptyBarCharacter($char)
260     {
261         $this->emptyBarChar = $char;
262     }
263
264     /**
265      * Gets the empty bar character.
266      *
267      * @return string A character
268      */
269     public function getEmptyBarCharacter()
270     {
271         return $this->emptyBarChar;
272     }
273
274     /**
275      * Sets the progress bar character.
276      *
277      * @param string $char A character
278      */
279     public function setProgressCharacter($char)
280     {
281         $this->progressChar = $char;
282     }
283
284     /**
285      * Gets the progress bar character.
286      *
287      * @return string A character
288      */
289     public function getProgressCharacter()
290     {
291         return $this->progressChar;
292     }
293
294     /**
295      * Sets the progress bar format.
296      *
297      * @param string $format The format
298      */
299     public function setFormat($format)
300     {
301         $this->format = null;
302         $this->internalFormat = $format;
303     }
304
305     /**
306      * Sets the redraw frequency.
307      *
308      * @param int|float $freq The frequency in steps
309      */
310     public function setRedrawFrequency($freq)
311     {
312         $this->redrawFreq = max((int) $freq, 1);
313     }
314
315     /**
316      * Starts the progress output.
317      *
318      * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged
319      */
320     public function start($max = null)
321     {
322         $this->startTime = time();
323         $this->step = 0;
324         $this->percent = 0.0;
325
326         if (null !== $max) {
327             $this->setMaxSteps($max);
328         }
329
330         $this->display();
331     }
332
333     /**
334      * Advances the progress output X steps.
335      *
336      * @param int $step Number of steps to advance
337      */
338     public function advance($step = 1)
339     {
340         $this->setProgress($this->step + $step);
341     }
342
343     /**
344      * Sets whether to overwrite the progressbar, false for new line.
345      *
346      * @param bool $overwrite
347      */
348     public function setOverwrite($overwrite)
349     {
350         $this->overwrite = (bool) $overwrite;
351     }
352
353     /**
354      * Sets the current progress.
355      *
356      * @param int $step The current progress
357      */
358     public function setProgress($step)
359     {
360         $step = (int) $step;
361
362         if ($this->max && $step > $this->max) {
363             $this->max = $step;
364         } elseif ($step < 0) {
365             $step = 0;
366         }
367
368         $prevPeriod = (int) ($this->step / $this->redrawFreq);
369         $currPeriod = (int) ($step / $this->redrawFreq);
370         $this->step = $step;
371         $this->percent = $this->max ? (float) $this->step / $this->max : 0;
372         if ($prevPeriod !== $currPeriod || $this->max === $step) {
373             $this->display();
374         }
375     }
376
377     /**
378      * Finishes the progress output.
379      */
380     public function finish()
381     {
382         if (!$this->max) {
383             $this->max = $this->step;
384         }
385
386         if ($this->step === $this->max && !$this->overwrite) {
387             // prevent double 100% output
388             return;
389         }
390
391         $this->setProgress($this->max);
392     }
393
394     /**
395      * Outputs the current progress string.
396      */
397     public function display()
398     {
399         if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
400             return;
401         }
402
403         if (null === $this->format) {
404             $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
405         }
406
407         $this->overwrite($this->buildLine());
408     }
409
410     /**
411      * Removes the progress bar from the current line.
412      *
413      * This is useful if you wish to write some output
414      * while a progress bar is running.
415      * Call display() to show the progress bar again.
416      */
417     public function clear()
418     {
419         if (!$this->overwrite) {
420             return;
421         }
422
423         if (null === $this->format) {
424             $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
425         }
426
427         $this->overwrite('');
428     }
429
430     /**
431      * Sets the progress bar format.
432      *
433      * @param string $format The format
434      */
435     private function setRealFormat($format)
436     {
437         // try to use the _nomax variant if available
438         if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
439             $this->format = self::getFormatDefinition($format.'_nomax');
440         } elseif (null !== self::getFormatDefinition($format)) {
441             $this->format = self::getFormatDefinition($format);
442         } else {
443             $this->format = $format;
444         }
445
446         $this->formatLineCount = substr_count($this->format, "\n");
447     }
448
449     /**
450      * Sets the progress bar maximal steps.
451      *
452      * @param int $max The progress bar max steps
453      */
454     private function setMaxSteps($max)
455     {
456         $this->max = max(0, (int) $max);
457         $this->stepWidth = $this->max ? Helper::strlen($this->max) : 4;
458     }
459
460     /**
461      * Overwrites a previous message to the output.
462      *
463      * @param string $message The message
464      */
465     private function overwrite($message)
466     {
467         if ($this->overwrite) {
468             if (!$this->firstRun) {
469                 // Move the cursor to the beginning of the line
470                 $this->output->write("\x0D");
471
472                 // Erase the line
473                 $this->output->write("\x1B[2K");
474
475                 // Erase previous lines
476                 if ($this->formatLineCount > 0) {
477                     $this->output->write(str_repeat("\x1B[1A\x1B[2K", $this->formatLineCount));
478                 }
479             }
480         } elseif ($this->step > 0) {
481             $this->output->writeln('');
482         }
483
484         $this->firstRun = false;
485
486         $this->output->write($message);
487     }
488
489     private function determineBestFormat()
490     {
491         switch ($this->output->getVerbosity()) {
492             // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
493             case OutputInterface::VERBOSITY_VERBOSE:
494                 return $this->max ? 'verbose' : 'verbose_nomax';
495             case OutputInterface::VERBOSITY_VERY_VERBOSE:
496                 return $this->max ? 'very_verbose' : 'very_verbose_nomax';
497             case OutputInterface::VERBOSITY_DEBUG:
498                 return $this->max ? 'debug' : 'debug_nomax';
499             default:
500                 return $this->max ? 'normal' : 'normal_nomax';
501         }
502     }
503
504     private static function initPlaceholderFormatters()
505     {
506         return array(
507             'bar' => function (ProgressBar $bar, OutputInterface $output) {
508                 $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth());
509                 $display = str_repeat($bar->getBarCharacter(), $completeBars);
510                 if ($completeBars < $bar->getBarWidth()) {
511                     $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
512                     $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
513                 }
514
515                 return $display;
516             },
517             'elapsed' => function (ProgressBar $bar) {
518                 return Helper::formatTime(time() - $bar->getStartTime());
519             },
520             'remaining' => function (ProgressBar $bar) {
521                 if (!$bar->getMaxSteps()) {
522                     throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
523                 }
524
525                 if (!$bar->getProgress()) {
526                     $remaining = 0;
527                 } else {
528                     $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress()));
529                 }
530
531                 return Helper::formatTime($remaining);
532             },
533             'estimated' => function (ProgressBar $bar) {
534                 if (!$bar->getMaxSteps()) {
535                     throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
536                 }
537
538                 if (!$bar->getProgress()) {
539                     $estimated = 0;
540                 } else {
541                     $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps());
542                 }
543
544                 return Helper::formatTime($estimated);
545             },
546             'memory' => function (ProgressBar $bar) {
547                 return Helper::formatMemory(memory_get_usage(true));
548             },
549             'current' => function (ProgressBar $bar) {
550                 return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
551             },
552             'max' => function (ProgressBar $bar) {
553                 return $bar->getMaxSteps();
554             },
555             'percent' => function (ProgressBar $bar) {
556                 return floor($bar->getProgressPercent() * 100);
557             },
558         );
559     }
560
561     private static function initFormats()
562     {
563         return array(
564             'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
565             'normal_nomax' => ' %current% [%bar%]',
566
567             'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
568             'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
569
570             'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%',
571             'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
572
573             'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
574             'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
575         );
576     }
577
578     /**
579      * @return string
580      */
581     private function buildLine()
582     {
583         $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
584         $callback = function ($matches) {
585             if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
586                 $text = \call_user_func($formatter, $this, $this->output);
587             } elseif (isset($this->messages[$matches[1]])) {
588                 $text = $this->messages[$matches[1]];
589             } else {
590                 return $matches[0];
591             }
592
593             if (isset($matches[2])) {
594                 $text = sprintf('%'.$matches[2], $text);
595             }
596
597             return $text;
598         };
599         $line = preg_replace_callback($regex, $callback, $this->format);
600
601         // gets string length for each sub line with multiline format
602         $linesLength = array_map(function ($subLine) {
603             return Helper::strlenWithoutDecoration($this->output->getFormatter(), rtrim($subLine, "\r"));
604         }, explode("\n", $line));
605
606         $linesWidth = max($linesLength);
607
608         $terminalWidth = $this->terminal->getWidth();
609         if ($linesWidth <= $terminalWidth) {
610             return $line;
611         }
612
613         $this->setBarWidth($this->barWidth - $linesWidth + $terminalWidth);
614
615         return preg_replace_callback($regex, $callback, $this->format);
616     }
617 }