8000 [Console] added a way to globally add a progress bar format or modify… · symfony/symfony@244d3b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 244d3b8

Browse files
committed
[Console] added a way to globally add a progress bar format or modify a built-in one
1 parent a9d47eb commit 244d3b8

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

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

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222
*/
2323
class ProgressBar
2424
{
25-
const FORMAT_QUIET = ' %percent%%';
26-
const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent:3s%%';
27-
const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%';
28-
const FORMAT_QUIET_NOMAX = ' %current%';
29-
const FORMAT_NORMAL_NOMAX = ' %current% [%bar%]';
30-
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed:6s%';
31-
3225
// options
3326
private $barWidth = 28;
3427
private $barChar = '=';
@@ -52,6 +45,7 @@ class ProgressBar
5245
private $messages;
5346

5447
static private $formatters;
48+
static private $formats;
5549

5650
/**
5751
* Constructor.
@@ -69,6 +63,10 @@ public function __construct(OutputInterface $output, $max = 0)
6963
if (!self::$formatters) {
7064
self::$formatters = self::initPlaceholderFormatters();
7165
}
66+
67+
if (!self::$formats) {
68+
self::$formats = self::initFormats();
69+
}
7270
}
7371

7472
/**
@@ -79,7 +77,7 @@ public function __construct(OutputInterface $output, $max = 0)
7977
* @param string $name The placeholder name (including the delimiter char like %)
8078
* @param callable $callable A PHP callable
8179
*/
82-
public static function setPlaceholderFormatter($name, $callable)
80+
public static function setPlaceholderFormatterDefinition($name, $callable)
8381
{
8482
if (!self::$formatters) {
8583
self::$formatters = self::initPlaceholderFormatters();
@@ -88,6 +86,23 @@ public static function setPlaceholderFormatter($name, $callable)
8886
self::$formatters[$name] = $callable;
8987
}
9088

89+
/**
90+
* Sets a format for a given name.
91+
*
92+
* This method also allow you to override an existing format.
93+
*
94+
* @param string $name The format name
95+
* @param string $format A format string
96+
*/
97+
public static function setFormatDefinition($name, $format)
98+
{
99+
if (!self::$formats) {
100+
self::$formats = self::initFormats();
101+
}
102+
103+
self::$formats[$name] = $format;
104+
}
105+
91106
public function setMessage($message, $name = 'message')
92107
{
93108
$this->messages[$name] = $message;
@@ -235,8 +250,8 @@ public function getProgressCharacter()
235250
*/
236251
public function setFormat($format)
237252
{
238-
$this->format = $format;
239-
$this->formatLineCount = substr_count($format, "\n");
253+
$this->format = isset(self::$formats[$format]) ? self::$formats[$format] : $format;
254+
$this->formatLineCount = substr_count($this->format, "\n");
240255
}
241256

242257
/**
@@ -408,28 +423,14 @@ private function determineBestFormat()
408423
{
409424
switch ($this->output->getVerbosity()) {
410425
case OutputInterface::VERBOSITY_QUIET:
411-
$format = self::FORMAT_QUIET_NOMAX;
412-
if ($this->max > 0) {
413-
$format = self::FORMAT_QUIET;
414-
}
415-
break;
426+
return $this->max > 0 ? 'quiet' : 'quiet_nomax';
416427
case OutputInterface::VERBOSITY_VERBOSE:
417428
case OutputInterface::VERBOSITY_VERY_VERBOSE:
418429
case OutputInterface::VERBOSITY_DEBUG:
419-
$format = self::FORMAT_VERBOSE_NOMAX;
420-
if ($this->max > 0) {
421-
$format = self::FORMAT_VERBOSE;
422-
}
423-
break;
430+
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
424431
default:
425-
$format = self::FORMAT_NORMAL_NOMAX;
426-
if ($this->max > 0) {
427-
$format = self::FORMAT_NORMAL;
428-
}
429-
break;
432+
return $this->max > 0 ? 'normal' : 'normal_nomax';
430433
}
431-
432-
return $format;
433434
}
434435

435436
static private function initPlaceholderFormatters()
@@ -488,4 +489,16 @@ static private function initPlaceholderFormatters()
488489
},
489490
);
490491
}
492+
493+
static private function initFormats()
494+
{
495+
return array(
496+
'quiet' => ' %percent%%',
497+
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
498+
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%',
499+
'quiet_nomax' => ' %current%',
500+
'normal_nomax' => ' %current% [%bar%]',
501+
'verbose_nomax' => ' %current% [%bar%] Elapsed: %elapsed:6s%',
502+
);
503+
}
491504
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function testParallelBars()
300300

301301
public function testAddingPlaceholderFormatter()
302302
{
303-
ProgressBar::setPlaceholderFormatter('remaining_steps', function (ProgressBar $bar) {
303+
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
304304
return $bar->getMaxSteps() - $bar->getStep();
305305
});
306306
$bar = new ProgressBar($output = $this->getOutputStream(), 3);

0 commit comments

Comments
 (0)
0