12 namespace Symfony\Component\Console\Helper;
30 const FORMAT_VERBOSE =
' %current%/%max% [%bar%] %percent%% Elapsed: %elapsed%';
112 array(59,
'secs', 1),
114 array(3600,
'mins', 60),
116 array(86400,
'hrs', 3600),
117 array(129600,
'1 day'),
118 array(604800,
'days', 86400),
123 if ($triggerDeprecationError) {
124 @trigger_error(
'The '.__CLASS__.
' class is deprecated since version 2.5 and will be removed in 3.0. Use the Symfony\Component\Console\Helper\ProgressBar class instead.', E_USER_DEPRECATED);
135 $this->barWidth = (int) $size;
145 $this->barChar = $char;
155 $this->emptyBarChar = $char;
165 $this->progressChar = $char;
185 $this->redrawFreq = (int) $freq;
196 $this->startTime = time();
198 $this->max = (int)
$max;
202 $this->lastMessagesLength = 0;
203 $this->barCharOriginal =
'';
205 if (null === $this->format) {
208 $this->format = self::FORMAT_QUIET_NOMAX;
209 if ($this->max > 0) {
210 $this->format = self::FORMAT_QUIET;
216 $this->format = self::FORMAT_VERBOSE_NOMAX;
217 if ($this->max > 0) {
218 $this->format = self::FORMAT_VERBOSE;
222 $this->format = self::FORMAT_NORMAL_NOMAX;
223 if ($this->max > 0) {
224 $this->format = self::FORMAT_NORMAL;
241 public function advance($step = 1, $redraw =
false)
243 $this->
setCurrent($this->current + $step, $redraw);
256 if (null === $this->startTime) {
257 throw new \LogicException(
'You must start the progress bar before calling setCurrent().');
262 if ($current < $this->current) {
263 throw new \LogicException(
'You can\'t regress the progress bar');
266 if (0 === $this->current) {
270 $prevPeriod = (int) ($this->current / $this->redrawFreq);
274 $currPeriod = (int) ($this->current / $this->redrawFreq);
275 if ($redraw || $prevPeriod !== $currPeriod || $this->max === $this->current) {
289 if (null === $this->startTime) {
290 throw new \LogicException(
'You must start the progress bar before calling display().');
294 foreach ($this->
generate($finish) as $name => $value) {
295 $message = str_replace(
"%{$name}%", $value, $message);
297 $this->
overwrite($this->output, $message);
317 if (null === $this->startTime) {
318 throw new \LogicException(
'You must start the progress bar before calling finish().');
321 if (null !== $this->startTime) {
326 $this->startTime = null;
327 $this->output->writeln(
'');
328 $this->output = null;
337 $this->formatVars = array();
338 foreach ($this->defaultFormatVars as $var) {
339 if (
false !== strpos($this->format,
"%{$var}%")) {
340 $this->formatVars[$var] =
true;
344 if ($this->max > 0) {
345 $this->widths[
'max'] = $this->
strlen($this->max);
346 $this->widths[
'current'] = $this->widths[
'max'];
364 if ($this->max > 0) {
365 $percent = (float) $this->current / $this->max;
368 if (isset($this->formatVars[
'bar'])) {
371 if ($this->max > 0) {
372 $completeBars = floor($percent * $this->barWidth);
375 $completeBars = floor($this->current % $this->barWidth);
381 $emptyBars = $this->barWidth - $completeBars - $this->
strlen($this->progressChar);
382 $bar = str_repeat($this->barChar, $completeBars);
383 if ($completeBars < $this->barWidth) {
385 $bar .= str_repeat($this->emptyBarChar, $emptyBars);
391 if (isset($this->formatVars[
'elapsed'])) {
393 $vars[
'elapsed'] = str_pad($this->
humaneTime($elapsed), $this->widths[
'elapsed'],
' ', STR_PAD_LEFT);
396 if (isset($this->formatVars[
'current'])) {
397 $vars[
'current'] = str_pad($this->current, $this->widths[
'current'],
' ', STR_PAD_LEFT);
400 if (isset($this->formatVars[
'max'])) {
404 if (isset($this->formatVars[
'percent'])) {
405 $vars[
'percent'] = str_pad(floor($percent * 100), $this->widths[
'percent'],
' ', STR_PAD_LEFT);
421 foreach ($this->timeFormats as
$format) {
422 if ($secs < $format[0]) {
423 if (count($format) == 2) {
427 $text = ceil($secs / $format[2]).
' '.$format[1];
444 $length = $this->
strlen($message);
447 if (null !== $this->lastMessagesLength && $this->lastMessagesLength > $length) {
448 $message = str_pad($message, $this->lastMessagesLength,
"\x20", STR_PAD_RIGHT);
452 $output->
write(
"\x0D");
453 $output->
write($message);
455 $this->lastMessagesLength = $this->
strlen($message);