4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\LogicException;
16 use Symfony\Component\Console\Output\OutputInterface;
19 * @author Kevin Bond <kevinbond@gmail.com>
21 class ProgressIndicator
27 private $indicatorValues;
28 private $indicatorCurrent;
29 private $indicatorChangeInterval;
30 private $indicatorUpdateTime;
31 private $started = false;
33 private static $formatters;
34 private static $formats;
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
42 public function __construct(OutputInterface $output, $format = null, $indicatorChangeInterval = 100, $indicatorValues = null)
44 $this->output = $output;
46 if (null === $format) {
47 $format = $this->determineBestFormat();
50 if (null === $indicatorValues) {
51 $indicatorValues = array('-', '\\', '|', '/');
54 $indicatorValues = array_values($indicatorValues);
56 if (2 > \count($indicatorValues)) {
57 throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
60 $this->format = self::getFormatDefinition($format);
61 $this->indicatorChangeInterval = $indicatorChangeInterval;
62 $this->indicatorValues = $indicatorValues;
63 $this->startTime = time();
67 * Sets the current indicator message.
69 * @param string|null $message
71 public function setMessage($message)
73 $this->message = $message;
79 * Starts the indicator output.
83 public function start($message)
86 throw new LogicException('Progress indicator already started.');
89 $this->message = $message;
90 $this->started = true;
91 $this->startTime = time();
92 $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
93 $this->indicatorCurrent = 0;
99 * Advances the indicator.
101 public function advance()
103 if (!$this->started) {
104 throw new LogicException('Progress indicator has not yet been started.');
107 if (!$this->output->isDecorated()) {
111 $currentTime = $this->getCurrentTimeInMilliseconds();
113 if ($currentTime < $this->indicatorUpdateTime) {
117 $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
118 ++$this->indicatorCurrent;
124 * Finish the indicator with message.
128 public function finish($message)
130 if (!$this->started) {
131 throw new LogicException('Progress indicator has not yet been started.');
134 $this->message = $message;
136 $this->output->writeln('');
137 $this->started = false;
141 * Gets the format for a given name.
143 * @param string $name The format name
145 * @return string|null A format string
147 public static function getFormatDefinition($name)
149 if (!self::$formats) {
150 self::$formats = self::initFormats();
153 return isset(self::$formats[$name]) ? self::$formats[$name] : null;
157 * Sets a placeholder formatter for a given name.
159 * This method also allow you to override an existing placeholder.
161 * @param string $name The placeholder name (including the delimiter char like %)
162 * @param callable $callable A PHP callable
164 public static function setPlaceholderFormatterDefinition($name, $callable)
166 if (!self::$formatters) {
167 self::$formatters = self::initPlaceholderFormatters();
170 self::$formatters[$name] = $callable;
174 * Gets the placeholder formatter for a given name.
176 * @param string $name The placeholder name (including the delimiter char like %)
178 * @return callable|null A PHP callable
180 public static function getPlaceholderFormatterDefinition($name)
182 if (!self::$formatters) {
183 self::$formatters = self::initPlaceholderFormatters();
186 return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
189 private function display()
191 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
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);
206 private function determineBestFormat()
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';
216 return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
221 * Overwrites a previous message to the output.
223 * @param string $message The message
225 private function overwrite($message)
227 if ($this->output->isDecorated()) {
228 $this->output->write("\x0D\x1B[2K");
229 $this->output->write($message);
231 $this->output->writeln($message);
235 private function getCurrentTimeInMilliseconds()
237 return round(microtime(true) * 1000);
240 private static function initPlaceholderFormatters()
243 'indicator' => function (ProgressIndicator $indicator) {
244 return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
246 'message' => function (ProgressIndicator $indicator) {
247 return $indicator->message;
249 'elapsed' => function (ProgressIndicator $indicator) {
250 return Helper::formatTime(time() - $indicator->startTime);
252 'memory' => function () {
253 return Helper::formatMemory(memory_get_usage(true));
258 private static function initFormats()
261 'normal' => ' %indicator% %message%',
262 'normal_no_ansi' => ' %message%',
264 'verbose' => ' %indicator% %message% (%elapsed:6s%)',
265 'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
267 'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
268 'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',