Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9d47eb

Browse filesBrowse files
committed
[Console] added a way to add a custom message on a progress bar
1 parent 7a30e50 commit a9d47eb
Copy full SHA for a9d47eb

File tree

Expand file treeCollapse file tree

2 files changed

+44
-22
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-22
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/ProgressBar.php
+41-19Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
class ProgressBar
2424
{
2525
const FORMAT_QUIET = ' %percent%%';
26-
const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent%%';
27-
const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent%% Elapsed: %elapsed%';
26+
const FORMAT_NORMAL = ' %current%/%max% [%bar%] %percent:3s%%';
27+
const FORMAT_VERBOSE = ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%';
2828
const FORMAT_QUIET_NOMAX = ' %current%';
2929
const FORMAT_NORMAL_NOMAX = ' %current% [%bar%]';
30-
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed%';
30+
const FORMAT_VERBOSE_NOMAX = ' %current% [%bar%] Elapsed: %elapsed:6s%';
3131

3232
// options
3333
private $barWidth = 28;
@@ -49,6 +49,7 @@ class ProgressBar
4949
private $lastMessagesLength;
5050
private $barCharOriginal;
5151
private $formatLineCount;
52+
private $messages;
5253

5354
static private $formatters;
5455

@@ -87,6 +88,16 @@ public static function setPlaceholderFormatter($name, $callable)
8788
self::$formatters[$name] = $callable;
8889
}
8990

91+
public function setMessage($message, $name = 'message')
92+
{
93+
$this->messages[$name] = $message;
94+
}
95+
96+
public function getMessage($name = 'message')
97+
{
98+
return $this->messages[$name];
99+
}
100+
90101
/**
91102
* Gets the progress bar start time.
92103
*
@@ -337,10 +348,21 @@ public function display()
337348
throw new \LogicException('You must start the progress bar before calling display().');
338349
}
339350

340-
$regex = implode('|', array_keys(self::$formatters));
341351
$self = $this;
342-
$this->overwrite(preg_replace_callback("{($regex)}", function ($matches) use ($self) {
343-
return call_user_func(self::$formatters[$matches[1]], $self);
352+
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
353+
if (isset(self::$formatters[$matches[1]])) {
354+
$text = call_user_func(self::$formatters[$matches[1]], $self);
355+
} elseif (isset($this->messages[$matches[1]])) {
356+
$text = $this->messages[$matches[1]];
357+
} else {
358+
return $matches[0];
359+
}
360+
361+
if (isset($matches[2])) {
362+
$text = sprintf('%'.$matches[2], $text);
363+
}
364+
365+
return $text;
344366
}, $this->format));
345367
}
346368

@@ -413,7 +435,7 @@ private function determineBestFormat()
413435
static private function initPlaceholderFormatters()
414436
{
415437
return array(
416-
'%bar%' => function (ProgressBar $bar) {
438+
'bar' => function (ProgressBar $bar) {
417439
$completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getStep() % $bar->getBarWidth());
418440
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlen($bar->getProgressCharacter());
419441
$display = str_repeat($bar->getBarCharacter(), $completeBars);
@@ -423,10 +445,10 @@ static private function initPlaceholderFormatters()
423445

424446
return $display;
425447
},
426-
'%elapsed%' => function (ProgressBar $bar) {
427-
return str_pad(Helper::formatTime(time() - $bar->getStartTime()), 6, ' ', STR_PAD_LEFT);
448+
'elapsed' => function (ProgressBar $bar) {
449+
return Helper::formatTime(time() - $bar->getStartTime());
428450
},
429-
'%remaining%' => function (ProgressBar $bar) {
451+
'remaining' => function (ProgressBar $bar) {
430452
if (!$bar->getMaxSteps()) {
431453
throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
432454
}
@@ -437,9 +459,9 @@ static private function initPlaceholderFormatters()
437459
$remaining = round((time() - $bar->getStartTime()) / $bar->getStep() * ($bar->getMaxSteps() - $bar->getStep()));
438460
}
439461

440-
return str_pad(Helper::formatTime($remaining), 6, ' ', STR_PAD_LEFT);
462+
return Helper::formatTime($remaining);
441463
},
442-
'%estimated%' => function (ProgressBar $bar) {
464+
'estimated' => function (ProgressBar $bar) {
443465
if (!$bar->getMaxSteps()) {
444466
throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
445467
}
@@ -450,19 +472,19 @@ static private function initPlaceholderFormatters()
450472
$estimated = round((time() - $bar->getStartTime()) / $bar->getStep() * $bar->getMaxSteps());
451473
}
452474

453-
return str_pad(Helper::formatTime($estimated), 6, ' ', STR_PAD_LEFT);
475+
return Helper::formatTime($estimated);
454476
},
455-
'%memory%' => function (ProgressBar $bar) {
456-
return str_pad(Helper::formatMemory(memory_get_usage(true)), 6, ' ', STR_PAD_LEFT);;
477+
'memory' => function (ProgressBar $bar) {
478+
return Helper::formatMemory(memory_get_usage(true));
457479
},
458-
'%current%' => function (ProgressBar $bar) {
480+
'current' => function (ProgressBar $bar) {
459481
return str_pad($bar->getStep(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
460482
},
461-
'%max%' => function (ProgressBar $bar) {
483+
'max' => function (ProgressBar $bar) {
462484
return $bar->getMaxSteps();
463485
},
464-
'%percent%' => function (ProgressBar $bar) {
465-
return str_pad(floor($bar->getProgressPercent() * 100), 3, ' ', STR_PAD_LEFT);
486+
'percent' => function (ProgressBar $bar) {
487+
return floor($bar->getProgressPercent() * 100);
466488
},
467489
);
468490
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testCustomizations()
6969
$bar->setBarCharacter('_');
7070
$bar->setEmptyBarCharacter(' ');
7171
$bar->setProgressCharacter('/');
72-
$bar->setFormat(' %current%/%max% [%bar%] %percent%%');
72+
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
7373
$bar->start();
7474
$bar->advance();
7575

@@ -102,7 +102,7 @@ public function testPercent()
102102
public function testOverwriteWithShorterLine()
103103
{
104104
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
105-
$bar->setFormat(' %current%/%max% [%bar%] %percent%%');
105+
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
106106
$bar->start();
107107
$bar->display();
108108
$bar->advance();
@@ -300,7 +300,7 @@ public function testParallelBars()
300300

301301
public function testAddingPlaceholderFormatter()
302302
{
303-
ProgressBar::setPlaceholderFormatter('%remaining_steps%', function (ProgressBar $bar) {
303+
ProgressBar::setPlaceholderFormatter('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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.