Search code examples
phpsymfonysymfony-console

Symfony Progress Bar is being recreated on new lines


The Symfony Console progress bar is not advancing on the same line but is rather being created on a new line

 1/2 [==============>-------------]  50%
 ! [NOTE] No changes made to Categories/CategoriesSchema              


 2/2 [============================] 100%
 2/2 [============================] 100%

I was under the assumption the progress bar will simply just move on the same line until the operation is complete. This is my code

$io = new SymfonyStyle($input, $output);
$progressbar = new ProgressBar($output, count($elements));
$progressbar->start();

foreach ($elements as $element) {

     //work on element
     io->note("No changes made to ".ucfirst($name));

     $progressbar->advance();
     $io->newLine();
}

$progressbar->finish();

What I'm I doing wrong??


Solution

  • The progress advancement will always be on a new line if it is not autonomous, unless you clear it prior to writing to the io. [sic]

    If you want to output something while the progress bar is running, call clear() first. After you're done, call display() to show the progress bar again.

    So either write to your io prior to starting the progress bar.

    $io->note('No changes made to ' . ucfirst($name));
    $io->newLine();
    
    $progressbar->start();
    foreach ($elements as $element) {
        $progressbar->advance();
        sleep(1);
    }
    $progressbar->finish();
    

    or call clear() on the progress bar prior to writing to the io and call display() when finished.

    $progressbar->start();
    foreach ($elements as $element) {
        if (true /* put conditional here */) {
            $progressbar->clear(); //remove progress bar from display
            $io->note('No changes made to ' . ucfirst($name));
            $io->newLine();
            $progressbar->display(); //redraw progress bar in display
        }
        $progressbar->advance(); //move up a step
        sleep(1);
    }    
    $progressbar->finish();