Symfony/Console: 如何使用多个进度条?

Symfony/Console: How to use multiple progress bars?

我有一个 Symfony/Console 命令,它使用 Guzzle Pool. I already have Guzzle reporting the download progress 为每个文件一次下载多个文件,效果很好。

现在我想使用 Symfony/Console 中的 ProgressBar helper 对其进行改进。问题是我为 ProgressBar 找到的所有示例仅使用一个进度条。我需要几个独立的进度条 - 每个下载一个。你能给我一些提示吗?

我在这里找到了一些东西:[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("3[1A");
    usleep(100000);
    if ($i <= 10) {
        $bar1->advance();
    }
    print "\n";
    $bar2->advance();
}

效果:

在更新栏之前,您必须将控制台光标移动到适当的行(向上和向下)。但它有效。我确认。

Laravel 5.6 / linux

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();

$bar1 = new ProgressBar($output->section(), 10);
$bar2 = new ProgressBar($output->section(), 20);
$bar2->setProgressCharacter('#');
$bar1->start();
print "\n";
$bar2->start();

for ($i = 1; $i <= 20; $i++) {
    // up one line
    $output->write("3[1A");
    usleep(100000);
    if ($i <= 10) {
        $bar1->advance();
    }
    print "\n";
    $bar2->advance();
}

Symfony 4.1 无需手动光标控制即可支持此功能,请参阅 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);
}