Commit 554b28d
committed
feature #10356 [Console] A better progress bar (fabpot)
This PR was merged into the 2.5-dev branch.
Discussion
----------
[Console] A better progress bar
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #9573, #9574, #10187, #9951, related to #9788
| License | MIT
| Doc PR | symfony/symfony-docs#3626
TODO:
- [x] add some docs
See what this PR allows you to do easily:

## New ProgressBar class
First, this PR deprecates `ProgressHelper` in favor of `ProgressBar`. The main difference is that the new `ProgressBar` class represents a single progress bar, which allows for more than one bar to be displayed at a time:
```php
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
$output = new ConsoleOutput();
$bar1 = new ProgressBar($output, 10);
$bar2 = new ProgressBar($output, 20);
$bar2->setProgressCharacter('#');
$bar1->start();
print "\n";
$bar2->start();
for ($i = 1; $i <= 20; $i++) {
// up one line
$output->write("\033[1A");
usleep(100000);
if ($i <= 10) {
$bar1->advance();
}
print "\n";
$bar2->advance();
}
```
And here is how it looks like when run:

## Format Placeholders
This pull request refactors the way placeholders in the progress bar are managed. It is now possible to add new placeholders or replace existing ones:
```php
// set a new placeholder
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
return $bar->getMaxSteps() - $bar->getStep();
});
// change the behavior of an existing placeholder
ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) {
return $bar->getMaxSteps() ?: '~';
});
```
Several new built-in placeholders have also been added:
* `%remaining%`: Display the remaining time
* `%estimated%`: Display the estimated time of the whole "task"
* `%memory%`: Display the memory usage
## Formats
Formats can also be added (or built-in ones modified):
```php
ProgressBar::setFormatDefinition('simple', '%current%');
$bar->setFormat('simple');
// is equivalent to
$bar->setFormat('%current%');
```
Built-in formats are:
* `quiet`
* `normal`
* `verbose`
* `quiet_nomax`
* `normal_nomax`
* `verbose_nomax`
## Format Messages
You can also set arbitrary messages that depends on the progression in the progress bar:
```php
$bar = new ProgressBar($output, 10);
$bar->setFormat("%message% %current%/%max% [%bar%]");
$bar->setMessage('started');
$bar->start();
$bar->setMessage('advancing');
$bar->advance();
$bar->setMessage('finish');
$bar->finish();
```
You are not limited to a single message (`message` being just the default one):
```php
$bar = new ProgressBar($output, 10);
$bar->setFormat("%message% %current%/%max% [%bar%] %end%");
$bar->setMessage('started');
$bar->setMessage('', 'end');
$bar->start();
$bar->setMessage('advancing');
$bar->advance();
$bar->setMessage('finish');
$bar->setMessage('ended...', 'end');
$bar->finish();
```
## Multiline Formats
A progress bar can now span more than one line:
```php
$bar->setFormat("%current%/%max% [%bar%]\n%message%");
```
## Flexible Format Definitions
The hardcoded formatting for some placeholders (like `%percent%` or `%elapsed%`) have been removed in favor of a `sprintf`-like format:
```php
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%");
```
Notice the `%percent:3s%` placeholder. Here, `%3s` is going to be used when rendering the placeholder.
## ANSI colors and Emojis
The new progress bar output can now contain ANSI colors and.or Emojis (see the small video at the top of this PR).
Commits
-------
0d1a58c [Console] made formats even more flexible
8c0022b [Console] fixed progress bar when using ANSI colors and Emojis
38f7a6f [Console] fixed PHP comptability
244d3b8 [Console] added a way to globally add a progress bar format or modify a built-in one
a9d47eb [Console] added a way to add a custom message on a progress bar
7a30e50 [Console] added support for multiline formats in ProgressBar
1aa7b8c [Console] added more default placeholder formatters for the progress bar
2a78a09 [Console] refactored the progress bar to allow placeholder to be extensible
4e76aa3 [Console] added ProgressBar (to replace the stateful ProgressHelper class)File tree
7 files changed
+1061
-17
lines changed- src/Symfony/Component/Console
- Helper
- Tests/Helper
7 files changed
+1061
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
26 | 51 | | |
27 | 52 | | |
28 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | | - | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
62 | 121 | | |
0 commit comments