@@ -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
@@ -52,30 +51,16 @@ class ProgressBar
52
51
*
53
52
* @param OutputInterface $output An OutputInterface instance
54
53
* @param int $max Maximum steps (0 if unknown)
55
- *
56
- * @throws \InvalidArgumentException
57
54
*/
58
55
public function __construct (OutputInterface $ output , $ max = 0 )
59
56
{
60
57
// Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
61
58
$ this ->output = $ output ->isDecorated () ? $ output : new NullOutput ();
62
59
$ this ->setMaxSteps ($ max );
63
60
64
- if (!self ::$ formatters ) {
65
- self ::$ formatters = self ::initPlaceholderFormatters ();
66
- }
67
-
68
- if (!self ::$ formats ) {
69
- self ::$ formats = self ::initFormats ();
70
- }
71
-
72
61
$ this ->setFormat ($ this ->determineBestFormat ());
73
62
74
63
$ this ->startTime = time ();
75
- $ this ->step = 0 ;
76
- $ this ->percent = 0 ;
77
- $ this ->lastMessagesLength = 0 ;
78
- $ this ->barCharOriginal = '' ;
79
64
}
80
65
81
66
/**
@@ -164,25 +149,6 @@ public function getStartTime()
164
149
return $ this ->startTime ;
165
150
}
166
151
167
- /**
168
- * Sets the progress bar maximal steps.
169
- *
170
- * @param int The progress bar max steps
171
- *
172
- * @throws \InvalidArgumentException
173
- */
174
- public function setMaxSteps ($ max )
175
- {
176
- $ max = (int ) $ max ;
177
-
178
- if ($ max < 0 ) {
179
- throw new \InvalidArgumentException ('Max steps should be a positive integer, 0 or null. Got "%s". ' , $ max );
180
- }
181
-
182
- $ this ->max = $ max ;
183
- $ this ->stepWidth = $ this ->max > 0 ? Helper::strlen ($ this ->max ) : 4 ;
184
- }
185
-
186
152
/**
187
153
* Gets the progress bar maximal steps.
188
154
*
@@ -196,29 +162,29 @@ public function getMaxSteps()
196
162
/**
197
163
* Gets the progress bar step.
198
164
*
199
- * @deprecated since 2.6, to be removed in 3.0. Use {@link getCurrent ()} instead.
165
+ * @deprecated since 2.6, to be removed in 3.0. Use {@link getProgress ()} instead.
200
166
*
201
167
* @return int The progress bar step
202
168
*/
203
169
public function getStep ()
204
170
{
205
- return $ this ->getCurrent ();
171
+ return $ this ->getProgress ();
206
172
}
207
173
208
174
/**
209
- * Gets the progress bar step.
175
+ * Gets the current step position .
210
176
*
211
177
* @return int The progress bar step
212
178
*/
213
- public function getCurrent ()
179
+ public function getProgress ()
214
180
{
215
181
return $ this ->step ;
216
182
}
217
183
218
184
/**
219
185
* Gets the progress bar step width.
220
186
*
221
- * @deprecated since 2.6 , it will be marked private from 3.0 .
187
+ * @internal This method is public for PHP 5.3 compatibility , it should not be used .
222
188
*
223
189
* @return int The progress bar step width
224
190
*/
@@ -230,7 +196,7 @@ public function getStepWidth()
230
196
/**
231
197
* Gets the current progress bar percent.
232
198
*
233
- * @return int The current progress bar percent
199
+ * @return float The current progress bar percent
234
200
*/
235
201
public function getProgressPercent ()
236
202
{
@@ -274,6 +240,10 @@ public function setBarCharacter($char)
274
240
*/
275
241
public function getBarCharacter ()
276
242
{
243
+ if (null === $ this ->barChar ) {
244
+ return $ this ->max ? '= ' : $ this ->emptyBarChar ;
245
+ }
246
+
277
247
return $ this ->barChar ;
278
248
}
279
249
@@ -325,10 +295,10 @@ public function getProgressCharacter()
325
295
public function setFormat ($ format )
326
296
{
327
297
// try to use the _nomax variant if available
328
- if (!$ this ->max && isset ( self ::$ formats [ $ format .'_nomax ' ] )) {
329
- $ this ->format = self ::$ formats [ $ format .'_nomax ' ] ;
330
- } elseif (isset ( self ::$ formats [ $ format] )) {
331
- $ 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) ;
332
302
} else {
333
303
$ this ->format = $ format ;
334
304
}
@@ -349,17 +319,16 @@ public function setRedrawFrequency($freq)
349
319
/**
350
320
* Starts the progress output.
351
321
*
352
- * @param int $max Maximum Step (0 if unknown)
322
+ * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged
353
323
*/
354
- public function start ($ max = 0 )
324
+ public function start ($ max = null )
355
325
{
356
- if ( 0 !== $ max ) {
357
- $ this ->setMaxSteps ( $ max ) ;
358
- }
326
+ $ this -> startTime = time ();
327
+ $ this ->step = 0 ;
328
+ $ this -> percent = 0.0 ;
359
329
360
- if (!$ this ->max ) {
361
- $ this ->barCharOriginal = $ this ->barChar ;
362
- $ this ->barChar = $ this ->emptyBarChar ;
330
+ if (null !== $ max ) {
331
+ $ this ->setMaxSteps ($ max );
363
332
}
364
333
365
334
$ this ->display ();
@@ -374,31 +343,45 @@ public function start($max = 0)
374
343
*/
375
344
public function advance ($ step = 1 )
376
345
{
377
- $ this ->setCurrent ($ this ->step + $ step );
346
+ $ this ->setProgress ($ this ->step + $ step );
378
347
}
379
348
380
349
/**
381
350
* Sets the current progress.
382
351
*
352
+ * @deprecated since 2.6, to be removed in 3.0. Use {@link setProgress()} instead.
353
+ *
383
354
* @param int $step The current progress
384
355
*
385
356
* @throws \LogicException
386
357
*/
387
358
public function setCurrent ($ step )
359
+ {
360
+ $ this ->setProgress ($ step );
361
+ }
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 )
388
371
{
389
372
$ step = (int ) $ step ;
390
373
if ($ step < $ this ->step ) {
391
374
throw new \LogicException ('You can \'t regress the progress bar. ' );
392
375
}
393
376
394
- if ($ this ->max > 0 && $ step > $ this ->max ) {
377
+ if ($ this ->max && $ step > $ this ->max ) {
395
378
$ this ->max = $ step ;
396
379
}
397
380
398
381
$ prevPeriod = intval ($ this ->step / $ this ->redrawFreq );
399
382
$ currPeriod = intval ($ step / $ this ->redrawFreq );
400
383
$ this ->step = $ step ;
401
- $ this ->percent = $ this ->max > 0 ? (float ) $ this ->step / $ this ->max : 0 ;
384
+ $ this ->percent = $ this ->max ? (float ) $ this ->step / $ this ->max : 0 ;
402
385
if ($ prevPeriod !== $ currPeriod || $ this ->max === $ step ) {
403
386
$ this ->display ();
404
387
}
@@ -410,22 +393,14 @@ public function setCurrent($step)
410
393
public function finish ()
411
394
{
412
395
if (!$ this ->max ) {
413
- $ this ->barChar = $ this ->barCharOriginal ;
414
396
$ this ->max = $ this ->step ;
415
- $ this ->setCurrent ($ this ->max );
416
- $ this ->max = 0 ;
417
- $ this ->barChar = $ this ->emptyBarChar ;
418
- } else {
419
- $ this ->setCurrent ($ this ->max );
420
397
}
421
398
422
- $ this ->startTime = null ;
399
+ $ this ->setProgress ( $ this -> max ) ;
423
400
}
424
401
425
402
/**
426
403
* Outputs the current progress string.
427
- *
428
- * @throws \LogicException
429
404
*/
430
405
public function display ()
431
406
{
@@ -466,6 +441,17 @@ public function clear()
466
441
$ this ->overwrite (str_repeat ("\n" , $ this ->formatLineCount ));
467
442
}
468
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
+
469
455
/**
470
456
* Overwrites a previous message to the output.
471
457
*
@@ -505,21 +491,21 @@ private function determineBestFormat()
505
491
switch ($ this ->output ->getVerbosity ()) {
506
492
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
507
493
case OutputInterface::VERBOSITY_VERBOSE :
508
- return $ this ->max > 0 ? 'verbose ' : 'verbose_nomax ' ;
494
+ return $ this ->max ? 'verbose ' : 'verbose_nomax ' ;
509
495
case OutputInterface::VERBOSITY_VERY_VERBOSE :
510
- return $ this ->max > 0 ? 'very_verbose ' : 'very_verbose_nomax ' ;
496
+ return $ this ->max ? 'very_verbose ' : 'very_verbose_nomax ' ;
511
497
case OutputInterface::VERBOSITY_DEBUG :
512
- return $ this ->max > 0 ? 'debug ' : 'debug_nomax ' ;
498
+ return $ this ->max ? 'debug ' : 'debug_nomax ' ;
513
499
default :
514
- return $ this ->max > 0 ? 'normal ' : 'normal_nomax ' ;
500
+ return $ this ->max ? 'normal ' : 'normal_nomax ' ;
515
501
}
516
502
}
517
503
518
504
private static function initPlaceholderFormatters ()
519
505
{
520
506
return array (
521
507
'bar ' => function (ProgressBar $ bar , OutputInterface $ output ) {
522
- $ 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 ());
523
509
$ display = str_repeat ($ bar ->getBarCharacter (), $ completeBars );
524
510
if ($ completeBars < $ bar ->getBarWidth ()) {
525
511
$ emptyBars = $ bar ->getBarWidth () - $ completeBars - Helper::strlenWithoutDecoration ($ output ->getFormatter (), $ bar ->getProgressCharacter ());
@@ -536,10 +522,10 @@ private static function initPlaceholderFormatters()
536
522
throw new \LogicException ('Unable to display the remaining time if the maximum number of steps is not set. ' );
537
523
}
538
524
539
- if (!$ bar ->getStep ()) {
525
+ if (!$ bar ->getProgress ()) {
540
526
$ remaining = 0 ;
541
527
} else {
542
- $ remaining = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * ($ bar ->getMaxSteps () - $ bar ->getStep ()));
528
+ $ remaining = round ((time () - $ bar ->getStartTime ()) / $ bar ->getProgress () * ($ bar ->getMaxSteps () - $ bar ->getProgress ()));
543
529
}
544
530
545
531
return Helper::formatTime ($ remaining );
@@ -549,10 +535,10 @@ private static function initPlaceholderFormatters()
549
535
throw new \LogicException ('Unable to display the estimated time if the maximum number of steps is not set. ' );
550
536
}
551
537
552
- if (!$ bar ->getStep ()) {
538
+ if (!$ bar ->getProgress ()) {
553
539
$ estimated = 0 ;
554
540
} else {
555
- $ estimated = round ((time () - $ bar ->getStartTime ()) / $ bar ->getStep () * $ bar ->getMaxSteps ());
541
+ $ estimated = round ((time () - $ bar ->getStartTime ()) / $ bar ->getProgress () * $ bar ->getMaxSteps ());
556
542
}
557
543
558
544
return Helper::formatTime ($ estimated );
@@ -561,7 +547,7 @@ private static function initPlaceholderFormatters()
561
547
return Helper::formatMemory (memory_get_usage (true ));
562
548
},
563
549
'current ' => function (ProgressBar $ bar ) {
564
- return str_pad ($ bar ->getStep (), $ bar ->getStepWidth (), ' ' , STR_PAD_LEFT );
550
+ return str_pad ($ bar ->getProgress (), $ bar ->getStepWidth (), ' ' , STR_PAD_LEFT );
565
551
},
566
552
'max ' => function (ProgressBar $ bar ) {
567
553
return $ bar ->getMaxSteps ();
0 commit comments