When executing longer-running commands, it may be helpful to show progress
information, which updates as your command runs:

To display progress details, use the
ProgressBar, pass it a total
number of units, and advance the progress as the command executes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

useSymfony\Component\Console\Helper\ProgressBar;// creates a new progress bar (50 units)$progressBar=newProgressBar($output,50);// starts and displays the progress bar$progressBar->start();$i=0;while($i++<50){// ... do some work// advances the progress bar 1 unit$progressBar->advance();// you can also advance the progress bar by more than 1 unit// $progressBar->advance(3);}// ensures that the progress bar is at 100%$progressBar->finish();

Instead of advancing the bar by a number of steps (with the
advance() method),
you can also set the current progress by calling the
setProgress() method.

New in version 2.6: The setProgress() method was called setCurrent() prior to Symfony 2.6.

Caution

Prior to version 2.6, the progress bar only works if your platform
supports ANSI codes; on other platforms, no output is generated.

Tip

If your platform doesn't support ANSI codes, updates to the progress
bar are added as new lines. To prevent the output from being flooded,
adjust the
setRedrawFrequency()
accordingly. By default, when using a max, the redraw frequency
is set to 10% of your max.

New in version 2.6: The setRedrawFrequency() method was introduced in Symfony 2.6.

If you don't know the number of steps in advance, just omit the steps argument
when creating the ProgressBar
instance:

A progress bar format is a string that contains specific placeholders (a name
enclosed with the % character); the placeholders are replaced based on the
current progress of the bar. Here is a list of the built-in placeholders:

current: The current step;

max: The maximum number of steps (or 0 if no max is defined);

bar: The bar itself;

percent: The percentage of completion (not available if no max is defined);

elapsed: The time elapsed since the start of the progress bar;

remaining: The remaining time to complete the task (not available if no max is defined);

estimated: The estimated time to complete the task (not available if no max is defined);

memory: The current memory usage;

message: used to display arbitrary messages in the progress bar (as explained later).

For instance, here is how you could set the format to be the same as the
debug one:

Notice the :6s part added to some placeholders? That's how you can tweak
the appearance of the bar (formatting and alignment). The part after the colon
(:) is used to set the sprintf format of the string.

Instead of setting the format for a given instance of a progress bar, you can
also define global formats:

Amongst the placeholders, bar is a bit special as all the characters used
to display it can be customized:

1
2
3
4
5
6
7
8
9
10
11

// the finished part of the bar$progressBar->setBarCharacter('<comment>=</comment>');// the unfinished part of the bar$progressBar->setEmptyBarCharacter(' ');// the progress character$progressBar->setProgressCharacter('|');// the bar width$progressBar->setBarWidth(50);

Caution

For performance reasons, be careful if you set the total number of steps
to a high number. For example, if you're iterating over a large number of
items, consider setting the redraw frequency to a higher value by calling
setRedrawFrequency(),
so it updates on only some iterations:

1
2
3
4
5
6
7
8
9
10
11
12

$progressBar=newProgressBar($output,50000);$progressBar->start();// update every 100 iterations$progressBar->setRedrawFrequency(100);$i=0;while($i++<50000){// ... do some work$progressBar->advance();}

If you want to display some information that depends on the progress bar
display that are not available in the list of built-in placeholders, you can
create your own. Let's see how you can create a remaining_steps placeholder
that displays the number of remaining steps:

Progress bars define a placeholder called message to display arbitrary
messages. However, none of the built-in formats include that placeholder, so
before displaying these messages, you must define your own custom format: