1

Symfony 控制台进度条不是在同一行上前进,而是在新行上创建

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


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

我假设进度条只会在同一行上移动,直到操作完成。这是我的代码

$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();

我做错了什么??

4

1 回答 1

4

如果它不是自治的,则进度进度将始终在新行上,除非您在写入 io 之前将其清除。[原文如此]

如果您想在进度条运行时输出一些东西,请先调用clear()。完成后,调用display()再次显示进度条。

因此,要么在启动进度条之前写入您的 io。

$io->note('No changes made to ' . ucfirst($name));
$io->newLine();

$progressbar->start();
foreach ($elements as $element) {
    $progressbar->advance();
    sleep(1);
}
$progressbar->finish();

clear()在写入 io 之前调用进度条并display()在完成时调用。

$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();
于 2018-02-11T03:05:17.157 回答