comparison vendor/symfony/console/Helper/ProgressIndicator.php @ 0:4c8ae668cc8c

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