Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit244d3b8

Browse files
committed
[Console] added a way to globally add a progress bar format or modify a built-in one
1 parenta9d47eb commit244d3b8

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-
constFORMAT_QUIET =' %percent%%';
26-
constFORMAT_NORMAL =' %current%/%max% [%bar%] %percent:3s%%';
27-
constFORMAT_VERBOSE =' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%';
28-
constFORMAT_QUIET_NOMAX =' %current%';
29-
constFORMAT_NORMAL_NOMAX =' %current% [%bar%]';
30-
constFORMAT_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
staticprivate$formatters;
48+
staticprivate$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-
publicstaticfunctionsetPlaceholderFormatter($name,$callable)
80+
publicstaticfunctionsetPlaceholderFormatterDefinition($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+
publicstaticfunctionsetFormatDefinition($name,$format)
98+
{
99+
if (!self::$formats) {
100+
self::$formats =self::initFormats();
101+
}
102+
103+
self::$formats[$name] =$format;
104+
}
105+
91106
publicfunctionsetMessage($message,$name ='message')
92107
{
93108
$this->messages[$name] =$message;
@@ -235,8 +250,8 @@ public function getProgressCharacter()
235250
*/
236251
publicfunctionsetFormat($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
staticprivatefunctioninitPlaceholderFormatters()
@@ -488,4 +489,16 @@ static private function initPlaceholderFormatters()
488489
},
489490
);
490491
}
492+
493+
staticprivatefunctioninitFormats()
494+
{
495+
returnarray(
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
publicfunctiontestAddingPlaceholderFormatter()
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 =newProgressBar($output =$this->getOutputStream(),3);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp