11
11
12
12
namespace Symfony \Component \Console \Helper ;
13
13
14
- use Symfony \Component \Console \Output \NullOutput ;
15
14
use Symfony \Component \Console \Output \OutputInterface ;
16
15
17
16
/**
@@ -42,6 +41,7 @@ class ProgressBar
42
41
private $ lastMessagesLength = 0 ;
43
42
private $ formatLineCount ;
44
43
private $ messages ;
44
+ private $ overwrite = true ;
45
45
46
46
private static $ formatters ;
47
47
private static $ formats ;
@@ -54,10 +54,19 @@ class ProgressBar
54
54
*/
55
55
public function __construct (OutputInterface $ output , $ max = 0 )
56
56
{
57
- // Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
58
- $ this ->output = $ output ->isDecorated () ? $ output : new NullOutput ();
57
+ $ this ->output = $ output ;
59
58
$ this ->setMaxSteps ($ max );
60
59
60
+ if (!$ this ->output ->isDecorated ()) {
61
+ // disable overwrite when output does not support ANSI codes.
62
+ $ this ->overwrite = false ;
63
+
64
+ if ($ this ->max > 10 ) {
65
+ // set a reasonable redraw frequency so output isn't flooded
66
+ $ this ->setRedrawFrequency ($ max / 10 );
67
+ }
68
+ }
69
+
61
70
$ this ->setFormat ($ this ->determineBestFormat ());
62
71
63
72
$ this ->startTime = time ();
@@ -360,6 +369,16 @@ public function setCurrent($step)
360
369
$ this ->setProgress ($ step );
361
370
}
362
371
372
+ /**
373
+ * Sets whether to overwrite the progressbar, false for new line
374
+ *
375
+ * @param bool $overwrite
376
+ */
377
+ public function setOverwrite ($ overwrite )
378
+ {
379
+ $ this ->overwrite = (bool ) $ overwrite ;
380
+ }
381
+
363
382
/**
364
383
* Sets the current progress.
365
384
*
@@ -396,6 +415,11 @@ public function finish()
396
415
$ this ->max = $ this ->step ;
397
416
}
398
417
418
+ if ($ this ->step === $ this ->max && !$ this ->overwrite ) {
419
+ // prevent double 100% output
420
+ return ;
421
+ }
422
+
399
423
$ this ->setProgress ($ this ->max );
400
424
}
401
425
@@ -438,6 +462,10 @@ public function display()
438
462
*/
439
463
public function clear ()
440
464
{
465
+ if (!$ this ->overwrite ) {
466
+ return ;
467
+ }
468
+
441
469
$ this ->overwrite (str_repeat ("\n" , $ this ->formatLineCount ));
442
470
}
443
471
@@ -470,8 +498,14 @@ private function overwrite($message)
470
498
}
471
499
}
472
500
473
- // move back to the beginning of the progress bar before redrawing it
474
- $ this ->output ->write ("\x0D" );
501
+ if ($ this ->overwrite ) {
502
+ // move back to the beginning of the progress bar before redrawing it
503
+ $ this ->output ->write ("\x0D" );
504
+ } elseif ($ this ->step > 0 ) {
505
+ // move to new line
506
+ $ this ->output ->writeln ('' );
507
+ }
508
+
475
509
if ($ this ->formatLineCount ) {
476
510
$ this ->output ->write (sprintf ("\033[%dA " , $ this ->formatLineCount ));
477
511
}
0 commit comments