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?
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:
You must move the console cursor to the appropriate line (up and down) before updating the bar. But it works. I confirm.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With