Progressbar组件显示在Symfony的多个行上。

时间:2023-01-06 15:57:33

I use the progressbar component in a simple command task with Symfony2 (2.6.6).

我在Symfony2的简单命令任务中使用progressbar组件(2.6.6)。

My code is like that:

我的代码是这样的:

...
$progress = new ProgressBar($output, $total);
$progress->start();

if (($handler = fopen($file, "r")) !== FALSE) {
    while (($row = fgetcsv($handler, 1000, ",")) !== FALSE) {
        $this->whatever();
        $progress->advance();
    }
    fclose($handler);
    $progress->finish();
}
...

And the output looks like:

输出为:

  0/50 [>---------------------------]   0%
  5/50 [==>-------------------------]  10%
 10/50 [=====>----------------------]  20%
 15/50 [========>-------------------]  30%
 20/50 [===========>----------------]  40%
 25/50 [==============>-------------]  50%
 30/50 [================>-----------]  60%
 35/50 [===================>--------]  70%
 40/50 [======================>-----]  80%
 45/50 [=========================>--]  90%
 50/50 [============================] 100

The progress bar is not reloading itself, appears in a new line with each ->advance(). I'm sure that the function ->whatever(); don't do anything with the output.

进度条本身没有重新加载,在每一个->advance()的新行中出现。我确信函数>无论如何;不要对输出做任何事情。

Anyone know why this behavior? Thanks you!

有人知道这种行为的原因吗?谢谢你!

Sorry for my English

对不起,我的英语

3 个解决方案

#1


4  

You can use setOverwrite() when initializing the progress bar:

在初始化进度条时,可以使用setOverwrite():

$progress = new ProgressBar($output, $total);
$progress->setOverwrite(true);

$progress->start();
...

This defines whether to overwrite the progressbar, or to create new line line.http://api.symfony.com/3.0/Symfony/Component/Console/Helper/ProgressBar.html#method_setOverwrite

它定义了是覆盖progressbar,还是创建新的line.http://api.symfony.com/3.0/ symfony/component/console/helper/progressbar .html#method_setOverwrite

#2


0  

You'd rather use SymfonyStyle (sf >= 2.7) class since Console Helper are now deprecated.

您宁愿使用SymfonyStyle (sf >= 2.7)类,因为控制台助手现在已被弃用。

Here is some dummy example:

这里有一些虚拟的例子:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $console = new SymfonyStyle($input, $output);
    $console->title('Dummy progressBar example');
    $console->progressStart(100);
    for ($i = 0; $i < 100; $i++) {
        // do something
        sleep(1);
        $console->progressAdvance();
    }
    $console->progressFinish(); // force progress
    $console->success('Dummy progressBar example complete!');
}

#3


-1  

you can try $output->setDecorated(true);

你可以尝试输出美元- > setDecorated(真正的);

#1


4  

You can use setOverwrite() when initializing the progress bar:

在初始化进度条时,可以使用setOverwrite():

$progress = new ProgressBar($output, $total);
$progress->setOverwrite(true);

$progress->start();
...

This defines whether to overwrite the progressbar, or to create new line line.http://api.symfony.com/3.0/Symfony/Component/Console/Helper/ProgressBar.html#method_setOverwrite

它定义了是覆盖progressbar,还是创建新的line.http://api.symfony.com/3.0/ symfony/component/console/helper/progressbar .html#method_setOverwrite

#2


0  

You'd rather use SymfonyStyle (sf >= 2.7) class since Console Helper are now deprecated.

您宁愿使用SymfonyStyle (sf >= 2.7)类,因为控制台助手现在已被弃用。

Here is some dummy example:

这里有一些虚拟的例子:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $console = new SymfonyStyle($input, $output);
    $console->title('Dummy progressBar example');
    $console->progressStart(100);
    for ($i = 0; $i < 100; $i++) {
        // do something
        sleep(1);
        $console->progressAdvance();
    }
    $console->progressFinish(); // force progress
    $console->success('Dummy progressBar example complete!');
}

#3


-1  

you can try $output->setDecorated(true);

你可以尝试输出美元- > setDecorated(真正的);