Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 554b28d

Browse files
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: ![cwzpvk](https://f.cloud.github.com/assets/47313/2302190/30cd80e4-a170-11e3-8d88-80c4e4ca8b23.gif) ## 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: ![progress-bars](https://f.cloud.github.com/assets/47313/2300612/4465889a-a0fd-11e3-8bc2-b1d2a0f5dc3d.gif) ## 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)
2 parents 65c9aca + 0d1a58c commit 554b28d

File tree

7 files changed

+1061
-17
lines changed

7 files changed

+1061
-17
lines changed

UPGRADE-3.0.md

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ UPGRADE FROM 2.x to 3.0
2323
* The methods `isQuiet`, `isVerbose`, `isVeryVerbose` and `isDebug` were added
2424
to `Symfony\Component\Console\Output\OutputInterface`.
2525

26+
* `ProgressHelper` has been removed in favor of `ProgressBar`.
27+
28+
Before:
29+
30+
```
31+
$h = new ProgressHelper();
32+
$h->start($output, 10);
33+
for ($i = 1; $i < 5; $i++) {
34+
usleep(200000);
35+
$h->advance();
36+
}
37+
$h->finish();
38+
```
39+
40+
After:
41+
42+
```
43+
$bar = new ProgressBar($output, 10);
44+
$bar->start();
45+
for ($i = 1; $i < 5; $i++) {
46+
usleep(200000);
47+
$bar->advance();
48+
}
49+
```
50+
2651
### EventDispatcher
2752

2853
* The interface `Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface`

src/Symfony/Component/Console/CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
2.5.0
55
-----
66

7-
* added a way to set a default command instead of `ListCommand`
8-
* added a way to set the process name of a command
7+
* deprecated ProgressHelper in favor of ProgressBar
8+
* added a way to set a default command instead of `ListCommand`
9+
* added a way to set the process name of a command
910

1011
2.4.0
1112
-----

src/Symfony/Component/Console/Helper/Helper.php

+60-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14+
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
15+
1416
/**
1517
* Helper is the base class for all helper classes.
1618
*
@@ -47,7 +49,7 @@ public function getHelperSet()
4749
*
4850
* @return integer The length of the string
4951
*/
50-
protected function strlen($string)
52+
public static function strlen($string)
5153
{
5254
if (!function_exists('mb_strlen')) {
5355
return strlen($string);
@@ -59,4 +61,61 @@ protected function strlen($string)
5961

6062
return mb_strlen($string, $encoding);
6163
}
64+
65+
public static function formatTime($secs)
66+
{
67+
static $timeFormats = array(
68+
array(0, '< 1 sec'),
69+
array(2, '1 sec'),
70+
array(59, 'secs', 1),
71+
array(60, '1 min'),
72+
array(3600, 'mins', 60),
73+
array(5400, '1 hr'),
74+
array(86400, 'hrs', 3600),
75+
array(129600, '1 day'),
76+
array(604800, 'days', 86400),
77+
);
78+
79+
foreach ($timeFormats as $format) {
80+
if ($secs >= $format[0]) {
81+
continue;
82+
}
83+
84+
if (2 == count($format)) {
85+
return $format[1];
86+
}
87+
88+
return ceil($secs / $format[2]).' '.$format[1];
89+
}
90+
}
91+
92+
public static function formatMemory($memory)
93+
{
94+
if ($memory >= 1024 * 1024 * 1024) {
95+
return sprintf('%.1f GB', $memory / 1024 / 1024 / 1024);
96+
}
97+
98+
if ($memory >= 1024 * 1024) {
99+
return sprintf('%.1f MB', $memory / 1024 / 1024);
100+
}
101+
102+
if ($memory >= 1024) {
103+
return sprintf('%d kB', $memory / 1024);
104+
}
105+
106+
return sprintf('%d B', $memory);
107+
}
108+
109+
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
110+
{
111+
$isDecorated = $formatter->isDecorated();
112+
$formatter->setDecorated(false);
113+
// remove <...> formatting
114+
$string = $formatter->format($string);
115+
// remove already formatted characters
116+
$string = preg_replace("/\033\[[^m]*m/", '', $string);
117+
$formatter->setDecorated($isDecorated);
118+
119+
return self::strlen($string);
120+
}
62121
}

0 commit comments

Comments
 (0)