@@ -24,7 +24,7 @@ class ProgressBar
24
24
{
25
25
// options
26
26
private $ barWidth = 28 ;
27
- private $ barChar = ' = ' ;
27
+ private $ barChar ;
28
28
private $ emptyBarChar = '- ' ;
29
29
private $ progressChar = '> ' ;
30
30
private $ format = null ;
@@ -34,13 +34,12 @@ class ProgressBar
34
34
* @var OutputInterface
35
35
*/
36
36
private $ output ;
37
- private $ step ;
37
+ private $ step = 0 ;
38
38
private $ max ;
39
39
private $ startTime ;
40
40
private $ stepWidth ;
41
- private $ percent ;
42
- private $ lastMessagesLength ;
43
- private $ barCharOriginal ;
41
+ private $ percent = 0.0 ;
42
+ private $ lastMessagesLength = 0 ;
44
43
private $ formatLineCount ;
45
44
private $ messages ;
46
45
@@ -57,18 +56,11 @@ public function __construct(OutputInterface $output, $max = 0)
57
56
{
58
57
// Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
59
58
$ this ->output = $ output ->isDecorated () ? $ output : new NullOutput ();
60
- $ this ->max = (int ) $ max ;
61
- $ this ->stepWidth = $ this ->max > 0 ? Helper::strlen ($ this ->max ) : 4 ;
62
-
63
- if (!self ::$ formatters ) {
64
- self ::$ formatters = self ::initPlaceholderFormatters ();
65
- }
66
-
67
- if (!self ::$ formats ) {
68
- self ::$ formats = self ::initFormats ();
69
- }
59
+ $ this ->setMaxSteps ($ max );
70
60
71
61
$ this ->setFormat ($ this ->determineBestFormat ());
62
+
63
+ $ this ->startTime = time ();
72
64
}
73
65
74
66
/**
@@ -170,16 +162,30 @@ public function getMaxSteps()
170
162
/**
171
163
* Gets the progress bar step.
172
164
*
165
+ * @deprecated since 2.6, to be removed in 3.0. Use {@link getProgress()} instead.
166
+ *
173
167
* @return int The progress bar step
174
168
*/
175
169
public function getStep ()
170
+ {
171
+ return $ this ->getProgress ();
172
+ }
173
+
174
+ /**
175
+ * Gets the current step position.
176
+ *
177
+ * @return int The progress bar step
178
+ */
179
+ public function getProgress ()
176
180
{
177
181
return $ this ->step ;
178
182
}
179
183
180
184
/**
181
185
* Gets the progress bar step width.
182
186
*
187
+ * @internal This method is public for PHP 5.3 compatibility, it should not be used.
188
+ *
183
189
* @return int The progress bar step width
184
190
*/
185
191
public function getStepWidth ()
@@ -190,7 +196,7 @@ public function getStepWidth()
190
196
/**
191
197
* Gets the current progress bar percent.
192
198
*
193
- * @return int The current progress bar percent
199
+ * @return float The current progress bar percent
194
200
*/
195
201
public function getProgressPercent ()
196
202
{
@@ -234,6 +240,10 @@ public function setBarCharacter($char)
234
240
*/
235
241
public function getBarCharacter ()
236
242
{
243
+ if (null === $ this ->barChar ) {
244
+ return $ this ->max ? '= ' : $ this ->emptyBarChar ;
245
+ }
246
+
237
247
return $ this ->barChar ;
238
248
}
239
249
@@ -285,10 +295,10 @@ public function getProgressCharacter()
285
295
public function setFormat ($ format )
286
296
{
287
297
// try to use the _nomax variant if available
288
- if (!$ this ->max && isset ( self ::$ formats [ $ format .'_nomax ' ] )) {
289
- $ this ->format = self ::$ formats [ $ format .'_nomax ' ] ;
290
- } elseif (isset ( self ::$ formats [ $ format] )) {
291
- $ this ->format = self ::$ formats [ $ format] ;
298
+ if (!$ this ->max && null !== self ::getFormatDefinition ( $ format .'_nomax ' )) {
299
+ $ this ->format = self ::getFormatDefinition ( $ format .'_nomax ' ) ;
300
+ } elseif (null !== self ::getFormatDefinition ( $ format )) {
301
+ $ this ->format = self ::getFormatDefinition ( $ format) ;
292
302
} else {
293
303
$ this ->format = $ format ;
294
304
}
@@ -308,18 +318,17 @@ public function setRedrawFrequency($freq)
308
318
309
319
/**
310
320
* Starts the progress output.
321
+ *
322
+ * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged
311
323
*/
312
- public function start ()
324
+ public function start ($ max = null )
313
325
{
314
326
$ this ->startTime = time ();
315
327
$ this ->step = 0 ;
316
- $ this ->percent = 0 ;
317
- $ this ->lastMessagesLength = 0 ;
318
- $ this ->barCharOriginal = '' ;
328
+ $ this ->percent = 0.0 ;
319
329
320
- if (!$ this ->max ) {
321
- $ this ->barCharOriginal = $ this ->barChar ;
322
- $ this ->barChar = $ this ->emptyBarChar ;
330
+ if (null !== $ max ) {
331
+ $ this ->setMaxSteps ($ max );
323
332
}
324
333
325
334
$ this ->display ();
@@ -334,35 +343,45 @@ public function start()
334
343
*/
335
344
public function advance ($ step = 1 )
336
345
{
337
- $ this ->setCurrent ($ this ->step + $ step );
346
+ $ this ->setProgress ($ this ->step + $ step );
338
347
}
339
348
340
349
/**
341
350
* Sets the current progress.
342
351
*
352
+ * @deprecated since 2.6, to be removed in 3.0. Use {@link setProgress()} instead.
353
+ *
343
354
* @param int $step The current progress
344
355
*
345
356
* @throws \LogicException
346
357
*/
347
358
public function setCurrent ($ step )
348
359
{
349
- if (null === $ this ->startTime ) {
350
- throw new \LogicException ('You must start the progress bar before calling setCurrent(). ' );
351
- }
360
+ $ this ->setProgress ($ step );
361
+ }
352
362
363
+ /**
364
+ * Sets the current progress.
365
+ *
366
+ * @param int $step The current progress
367
+ *
368
+ * @throws \LogicException
369
+ */
370
+ public function setProgress ($ step )
371
+ {
353
372
$ step = (int ) $ step ;
354
373
if ($ step < $ this ->step ) {
355
374
throw new \LogicException ('You can \'t regress the progress bar. ' );
356
375
}
357
376
358
- if ($ this ->max > 0 && $ step > $ this ->max ) {
359
- throw new \ LogicException ( ' You can \' t advance the progress bar past the max value. ' ) ;
377
+ if ($ this ->max && $ step > $ this ->max ) {
378
+ $ this -> max = $ step ;
360
379
}
361
380
362
381
$ prevPeriod = intval ($ this ->step / $ this ->redrawFreq );
363
382
$ currPeriod = intval ($ step / $ this ->redrawFreq );
364
383
$ this ->step = $ step ;
365
- $ this ->percent = $ this ->max > 0 ? (float ) $ this ->step / $ this ->max : 0 ;
384
+ $ this ->percent = $ this ->max ? (float ) $ this ->step / $ this ->max : 0 ;
366
385
if ($ prevPeriod !== $ currPeriod || $ this ->max === $ step ) {
367
386
$ this ->display ();
368
387
}
@@ -373,32 +392,20 @@ public function setCurrent($step)
373
392
*/
374
393
public function finish ()
375
394
{
376
- if (null === $ this ->startTime ) {
377
- throw new \LogicException ('You must start the progress bar before calling finish(). ' );
378
- }
379
-
380
395
if (!$ this ->max ) {
381
- $ this ->barChar = $ this ->barCharOriginal ;
382
396
$ this ->max = $ this ->step ;
383
- $ this ->setCurrent ($ this ->max );
384
- $ this ->max = 0 ;
385
- $ this ->barChar = $ this ->emptyBarChar ;
386
- } else {
387
- $ this ->setCurrent ($ this ->max );
388
397
}
389
398
390
- $ this ->startTime = null ;
399
+ $ this ->setProgress ( $ this -> max ) ;
391
400
}
392
401
393
402
/**
394
403
* Outputs the current progress string.
395
- *
396
- * @throws \LogicException
397
404
*/
398
405
public function display ()
399
406
{
400
- if (null === $ this ->startTime ) {
401
- throw new \ LogicException ( ' You must start the progress bar before calling display(). ' ) ;
407
+ if (OutputInterface:: VERBOSITY_QUIET === $ this ->output -> getVerbosity () ) {
408
+ return ;
402
409
}
403
410
404
411
// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
@@ -434,6 +441,17 @@ public function clear()
434
441
$ this ->overwrite (str_repeat ("\n" , $ this ->formatLineCount ));
435
442
}
436
443
444
+ /**
445
+ * Sets the progress bar maximal steps.
446
+ *
447
+ * @param int The progress bar max steps
448
+ */
449
+ private function setMaxSteps ($ max )
450
+ {
451
+ $ this ->max = max (0 , (int ) $ max );
452
+ $ this ->stepWidth = $ this ->max ? Helper::strlen ($ this ->max ) : 4 ;
453
+ }
454
+
437
455
/**
438
456
* Overwrites a previous message to the output.
439
457
*
@@ -473,21 +491,21 @@ private function determineBestFormat()
473
491
switch ($ this ->output ->getVerbosity ()) {
474
492
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
475
493
case OutputInterface::VERBOSITY_VERBOSE :
476
- return $ this ->max > 0 ? 'verbose ' : 'verbose_nomax ' ;
494
+ return $ this ->max ? 'verbose ' : 'verbose_nomax ' ;
477
495
case OutputInterface::VERBOSITY_VERY_VERBOSE :
478
- return $ this ->max > 0 ? 'very_verbose ' : 'very_verbose_nomax ' ;
496
+ return $ this ->max ? 'very_verbose ' : 'very_verbose_nomax ' ;
479
497
case OutputInterface::VERBOSITY_DEBUG :
480
- return $ this ->max > 0 ? 'debug ' : 'debug_nomax ' ;
498
+ return $ this ->max ? 'debug ' : 'debug_nomax ' ;
481
499
default :
482
- return $ this ->max > 0 ? 'normal ' : 'normal_nomax ' ;
500
+ return $ this ->max ? 'normal ' : 'normal_nomax ' ;
483
501
}
484
502
}
485
503
486
504
private static function initPlaceholderFormatters ()
487
505
{
488
506
return array (
489
507
'bar ' => function (ProgressBar $ bar , OutputInterface $ output ) {
490
- $ completeBars = floor ($ bar ->getMaxSteps () > 0 ? $ bar ->getProgressPercent () * $ bar ->getBarWidth () : $ bar ->getStep () % $ bar ->getBarWidth ());
508
+ $ completeBars = floor ($ bar ->getMaxSteps () > 0 ? $ bar ->getProgressPercent () * $ bar ->getBarWidth () : $ bar ->getProgress () % $ bar ->getBarWidth ());
491
509
$ display = str_repeat ($ bar ->getBarCharacter (), $ completeBars );
492
510
if ($ completeBars < $ bar ->getBarWidth ()) {
493
511
$ emptyBars = $ bar ->getBarWidth () - $ completeBars - Helper::strlenWithoutDecoration ($ output ->getFormatter (), $ bar ->getProgressCharacter ());
@@ -504,10 +522,10 @@ private static function initPlaceholderFormatters()
504
522
throw new \LogicException ('Unable to display the remaining time if the maximum number of steps is not set. ' );
505
523
}
506
524
507
- if (!$ bar ->getStep ()) {
525
+ if (!$ bar ->getProgress ()) {
508
526
$ remaining = 0 ;
509
527
} else {
510
- $ remaining = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * ($ bar ->getMaxSteps () - $ bar ->getStep ()));
528
+ $ remaining = round ((time () - $ bar ->getStartTime ()) / $ bar ->getProgress () * ($ bar ->getMaxSteps () - $ bar ->getProgress ()));
511
529
}
512
530
513
531
return Helper::formatTime ($ remaining );
@@ -517,10 +535,10 @@ private static function initPlaceholderFormatters()
517
535
throw new \LogicException ('Unable to display the estimated time if the maximum number of steps is not set. ' );
518
536
}
519
537
520
- if (!$ bar ->getStep ()) {
538
+ if (!$ bar ->getProgress ()) {
521
539
$ estimated = 0 ;
522
540
} else {
523
- $ estimated = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * $ bar ->getMaxSteps ());
541
+ $ estimated = round ((time () - $ bar ->getStartTime ()) / $ bar ->getProgress () * $ bar ->getMaxSteps ());
524
542
}
525
543
526
544
return Helper::formatTime ($ estimated );
@@ -529,7 +547,7 @@ private static function initPlaceholderFormatters()
529
547
return Helper::formatMemory (memory_get_usage (true ));
530
548
},
531
549
'current ' => function (ProgressBar $ bar ) {
532
- return str_pad ($ bar ->getStep (), $ bar ->getStepWidth (), ' ' , STR_PAD_LEFT );
550
+ return str_pad ($ bar ->getProgress (), $ bar ->getStepWidth (), ' ' , STR_PAD_LEFT );
533
551
},
534
552
'max ' => function (ProgressBar $ bar ) {
535
553
return $ bar ->getMaxSteps ();
0 commit comments