Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 987
enumag Avatar asked Apr 15 '17 09:04

enumag


2 Answers

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.

like image 116
Gander Avatar answered Sep 18 '22 08:09

Gander


With Symfony 4.1 this is supported without manual cursor-control, see https://symfony.com/doc/current/components/console/helpers/progressbar.html#console-multiple-progress-bars:

$section1 = $output->section();
$section2 = $output->section();

$progress1 = new ProgressBar($section1);
$progress2 = new ProgressBar($section2);

$progress1->start(100);
$progress2->start(100);

$i = 0;
while (++$i < 100) {
    $progress1->advance();

    if ($i % 2 === 0) {
         $progress2->advance(4);
    }

    usleep(50000);
}
like image 35
Tim Riemenschneider Avatar answered Sep 21 '22 08:09

Tim Riemenschneider