23
23
class ProgressBar
24
24
{
25
25
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 % ' ;
28
28
const FORMAT_QUIET_NOMAX = ' %current% ' ;
29
29
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 % ' ;
31
31
32
32
// options
33
33
private $ barWidth = 28 ;
@@ -49,6 +49,7 @@ class ProgressBar
49
49
private $ lastMessagesLength ;
50
50
private $ barCharOriginal ;
51
51
private $ formatLineCount ;
52
+ private $ messages ;
52
53
53
54
static private $ formatters ;
54
55
@@ -87,6 +88,16 @@ public static function setPlaceholderFormatter($name, $callable)
87
88
self ::$ formatters [$ name ] = $ callable ;
88
89
}
89
90
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
+
90
101
/**
91
102
* Gets the progress bar start time.
92
103
*
@@ -337,10 +348,21 @@ public function display()
337
348
throw new \LogicException ('You must start the progress bar before calling display(). ' );
338
349
}
339
350
340
- $ regex = implode ('| ' , array_keys (self ::$ formatters ));
341
351
$ 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 ;
344
366
}, $ this ->format ));
345
367
}
346
368
@@ -413,7 +435,7 @@ private function determineBestFormat()
413
435
static private function initPlaceholderFormatters ()
414
436
{
415
437
return array (
416
- '% bar% ' => function (ProgressBar $ bar ) {
438
+ 'bar ' => function (ProgressBar $ bar ) {
417
439
$ completeBars = floor ($ bar ->getMaxSteps () > 0 ? $ bar ->getProgressPercent () * $ bar ->getBarWidth () : $ bar ->getStep () % $ bar ->getBarWidth ());
418
440
$ emptyBars = $ bar ->getBarWidth () - $ completeBars - Helper::strlen ($ bar ->getProgressCharacter ());
419
441
$ display = str_repeat ($ bar ->getBarCharacter (), $ completeBars );
@@ -423,10 +445,10 @@ static private function initPlaceholderFormatters()
423
445
424
446
return $ display ;
425
447
},
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 ());
428
450
},
429
- '% remaining% ' => function (ProgressBar $ bar ) {
451
+ 'remaining ' => function (ProgressBar $ bar ) {
430
452
if (!$ bar ->getMaxSteps ()) {
431
453
throw new \LogicException ('Unable to display the remaining time if the maximum number of steps is not set. ' );
432
454
}
@@ -437,9 +459,9 @@ static private function initPlaceholderFormatters()
437
459
$ remaining = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * ($ bar ->getMaxSteps () - $ bar ->getStep ()));
438
460
}
439
461
440
- return str_pad ( Helper::formatTime ($ remaining), 6 , ' ' , STR_PAD_LEFT );
462
+ return Helper::formatTime ($ remaining );
441
463
},
442
- '% estimated% ' => function (ProgressBar $ bar ) {
464
+ 'estimated ' => function (ProgressBar $ bar ) {
443
465
if (!$ bar ->getMaxSteps ()) {
444
466
throw new \LogicException ('Unable to display the estimated time if the maximum number of steps is not set. ' );
445
467
}
@@ -450,19 +472,19 @@ static private function initPlaceholderFormatters()
450
472
$ estimated = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * $ bar ->getMaxSteps ());
451
473
}
452
474
453
- return str_pad ( Helper::formatTime ($ estimated), 6 , ' ' , STR_PAD_LEFT );
475
+ return Helper::formatTime ($ estimated );
454
476
},
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 ));
457
479
},
458
- '% current% ' => function (ProgressBar $ bar ) {
480
+ 'current ' => function (ProgressBar $ bar ) {
459
481
return str_pad ($ bar ->getStep (), $ bar ->getStepWidth (), ' ' , STR_PAD_LEFT );
460
482
},
461
- '% max% ' => function (ProgressBar $ bar ) {
483
+ 'max ' => function (ProgressBar $ bar ) {
462
484
return $ bar ->getMaxSteps ();
463
485
},
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 );
466
488
},
467
489
);
468
490
}
0 commit comments