Skip to content

[Console] A better progress bar #10356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 3, 2014
25 changes: 25 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ UPGRADE FROM 2.x to 3.0
* The methods `isQuiet`, `isVerbose`, `isVeryVerbose` and `isDebug` were added
to `Symfony\Component\Console\Output\OutputInterface`.

* `ProgressHelper` has been removed in favor of `ProgressBar`.

Before:

```
$h = new ProgressHelper();
$h->start($output, 10);
for ($i = 1; $i < 5; $i++) {
usleep(200000);
$h->advance();
}
$h->finish();
```

After:

```
$bar = new ProgressBar($output, 10);
$bar->start();
for ($i = 1; $i < 5; $i++) {
usleep(200000);
$bar->advance();
}
```

### EventDispatcher

* The interface `Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface`
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ CHANGELOG
2.5.0
-----

* added a way to set a default command instead of `ListCommand`
* added a way to set the process name of a command
* deprecated ProgressHelper in favor of ProgressBar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the classnames not be included in an inline code block?

* added a way to set a default command instead of `ListCommand`
* added a way to set the process name of a command

2.4.0
-----
Expand Down
61 changes: 60 additions & 1 deletion src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Formatter\OutputFormatterInterface;

/**
* Helper is the base class for all helper classes.
*
Expand Down Expand Up @@ -47,7 +49,7 @@ public function getHelperSet()
*
* @return integer The length of the string
*/
protected function strlen($string)
public static function strlen($string)
{
if (!function_exists('mb_strlen')) {
return strlen($string);
Expand All @@ -59,4 +61,61 @@ protected function strlen($string)

return mb_strlen($string, $encoding);
}

public static function formatTime($secs)
{
static $timeFormats = array(
array(0, '< 1 sec'),
array(2, '1 sec'),
array(59, 'secs', 1),
array(60, '1 min'),
array(3600, 'mins', 60),
array(5400, '1 hr'),
array(86400, 'hrs', 3600),
array(129600, '1 day'),
array(604800, 'days', 86400),
);

foreach ($timeFormats as $format) {
if ($secs >= $format[0]) {
continue;
}

if (2 == count($format)) {
return $format[1];
}

return ceil($secs / $format[2]).' '.$format[1];
}
}

public static function formatMemory($memory)
{
if ($memory >= 1024 * 1024 * 1024) {
return sprintf('%.1f GB', $memory / 1024 / 1024 / 1024);
}

if ($memory >= 1024 * 1024) {
return sprintf('%.1f MB', $memory / 1024 / 1024);
}

if ($memory >= 1024) {
return sprintf('%d kB', $memory / 1024);
}

return sprintf('%d B', $memory);
}

public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
{
$isDecorated = $formatter->isDecorated();
$formatter->setDecorated(false);
// remove <...> formatting
$string = $formatter->format($string);
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string);
$formatter->setDecorated($isDecorated);

return self::strlen($string);
}
}
Loading