2323class 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 }
0 commit comments