yoye/progressr

该软件包最新版本(dev-master)没有可用的许可信息。

Symfony2控制台组件中进度状态的辅助工具

安装: 653

依赖者: 0

建议者: 0

安全: 0

星标: 3

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2013-02-19 16:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:12:51 UTC


README

自从Symfony 2.2控制台组件实现了自己的进度状态: https://symfony.com.cn/blog/new-in-symfony-2-2-better-interaction-from-the-console

简介

Progressr 是一个辅助工具,用于查看使用 Symfony2 控制台组件的命令的进展。您可以选择显示进展信息或进度条。

安装

安装 progressr 的最佳方式是使用 composer

用法

手动

安装完成后,您将获得一个辅助工具和一个迭代器,您可以选择如何使用 progressr。您可以手动显示信息

<?php

use Progressr\Console\Helper\Progress;

class FooCommand extends ContainerAwareCommand
{


    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $progress = new Progress($output, 10);
    
        foreach (range(1, 10) as $value) {
            $progress
                ->increment()
                ->display()
            ;
            
            // Will display "Current status: x/100 x%"
            
            sleep(1);
        }
    }
}

如果您的命令包含多个步骤,您可以手动设置当前位置。

<?php
$progress = new Progress($output, 4);
$progress->setCurrent(1)->display('Step1:'); // Display "Step1: 1/4 25%"
$progress->setCurrent(2)->display('Step2:'); // Display "Step2: 2/4 50%"
$progress->setCurrent(3)->display('Step3:'); // Display "Step3: 3/4 75%"
$progress->setCurrent(4)->display('Final step:'); // Display "Final step: 4/4 100%" and add new line

您还可以显示进度条。

<?php
$progress = new Progress($output, 4, Progress::FLAG_BAR);
$progress->setCurrent(1)->display(); // Display "[=====               ] 1/4 25%"

您还可以显示计时器。

<?php
$progress = new Progress($output, 4, Progress::FLAG_TIMER);
$progress->setCurrent(1)->display(); // Display "15min 25sec"

当然,您可以将所有这些混合在一起,如下所示

<?php
$progress = new Progress($output, 4, Progress::FLAG_TIMER | Progress::FLAG_BAR | Progress::FLAG_INFO);
$progress->setCurrent(1)->display('my message:'); // Display "my message: [=====               ] 15min 25sec 1/4 25%"

您不能重新定义显示顺序,消息始终首先显示,然后是进度条、经过的时间和信息。所有这些都是仅用于信息的东西,我认为没有人关心显示顺序。

迭代器

Progressr 可以与一个迭代器一起使用,该迭代器将自动显示信息。

<?php
$array = range(1, 100);
$iterator = new ProgressIterator($output, $array, Progress::FLAG_BAR | Progress::FLAG_INFO, 'my message:');

foreach ($iterator as $value) {
    // Do stuff
    // Progressr: Automatic display
}