Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progressbar component displayed on multiple lines in Symfony

I use the progressbar component in a simple command task with Symfony2 (2.6.6).

My code is like that:

...
$progress = new ProgressBar($output, $total);
$progress->start();

if (($handler = fopen($file, "r")) !== FALSE) {
    while (($row = fgetcsv($handler, 1000, ",")) !== FALSE) {
        $this->whatever();
        $progress->advance();
    }
    fclose($handler);
    $progress->finish();
}
...

And the output looks like:

  0/50 [>---------------------------]   0%
  5/50 [==>-------------------------]  10%
 10/50 [=====>----------------------]  20%
 15/50 [========>-------------------]  30%
 20/50 [===========>----------------]  40%
 25/50 [==============>-------------]  50%
 30/50 [================>-----------]  60%
 35/50 [===================>--------]  70%
 40/50 [======================>-----]  80%
 45/50 [=========================>--]  90%
 50/50 [============================] 100

The progress bar is not reloading itself, appears in a new line with each ->advance(). I'm sure that the function ->whatever(); don't do anything with the output.

Anyone know why this behavior? Thanks you!

Sorry for my English

like image 742
Carlos Vázquez Avatar asked Apr 17 '15 14:04

Carlos Vázquez


1 Answers

You can use setOverwrite() when initializing the progress bar:

$progress = new ProgressBar($output, $total);
$progress->setOverwrite(true);

$progress->start();
...

This defines whether to overwrite the progressbar, or to create new line line.http://api.symfony.com/3.0/Symfony/Component/Console/Helper/ProgressBar.html#method_setOverwrite

like image 165
Andrii Avatar answered Oct 08 '22 05:10

Andrii