Search code examples
phpsymfonyguzzlesymfony-console

Symfony/Console: How to use multiple progress bars?


I have a command for Symfony/Console which downloads several files at once using Guzzle Pool. I already have Guzzle reporting the download progress for each file, that works fine.

Now I'd like to improve it using the ProgressBar helper from Symfony/Console.The problem is that all examples I found for the ProgressBar only use a single progress bar. I need several independent progress bars - one for each of the downloads. Can you give me some hint how to achieve that?


Solution

  • I found something here: [Console] A better progress bar #10356

    use Symfony\Component\Console\Helper\ProgressBar;
    use Symfony\Component\Console\Output\ConsoleOutput;
    
    $output = new ConsoleOutput();
    
    $bar1 = new ProgressBar($output, 10);
    $bar2 = new ProgressBar($output, 20);
    $bar2->setProgressCharacter('#');
    $bar1->start();
    print "\n";
    $bar2->start();
    
    for ($i = 1; $i <= 20; $i++) {
        // up one line
        $output->write("\033[1A");
        usleep(100000);
        if ($i <= 10) {
            $bar1->advance();
        }
        print "\n";
        $bar2->advance();
    }
    

    Effect:

    ProgressBar

    You must move the console cursor to the appropriate line (up and down) before updating the bar. But it works. I confirm.