icanhazstring/symfony-console-spinner

自定义symfony进度条旋转器

1.1.1 2024-03-27 13:04 UTC

This package is auto-updated.

Last update: 2024-08-27 14:08:01 UTC


README

带有旋转器的自定义symfony进度条。

我想有一个进度条来显示长时间运行的任务,比如等待外部资源可用。这就是我发现了alecrabbit/php-cli-snake的地方。

我尝试使用symfony/console模仿这个动画,这是结果

example of multiple progress bars with this spinner component

安装

使用composer安装这个小包

composer require icanhazstring/symfony-console-spinner

用法

要使用旋转器,只需像symfony中的默认ProgressBar一样实例化SpinnerProgress,给它提供OutputInterface和要处理的项目最大计数

public function execute(OutputInterface $output, InputInterface $input)
{
    $spinner = new SpinnerProgress($output, 100);

    for($i = 0; $i < 100; $i++) {

        usleep(1000);

        $spinner->advance();

    }

    $spinner->finish();
}

如果您想更改某些内容,例如将经过的时间添加到输出格式中,您也可以获取底层的ProgressBar实例

$spinner = new SpinnerProgress($output, 100);
$spinner->getProgressBar()->setFormat('%bar% (%elapsed:6s%) %message%');

许可证

本软件包在MIT许可证下发布。