laravel 命令行输出进度条

有时候我们想在命令行执行一些耗时的命令,我们可以利用 symfony 提供的进度条相关的类,来输出一个进度条,显示当前的处理进度。

参考:http://symfony.com/doc/current/components/console/helpers/progressbar.html

<?php

namespace App\Commands;

use App\Console\Commands\BaseCommand;
use Illuminate\Contracts\Bus\SelfHandling;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class Test extends BaseCommand implements SelfHandling
{
    protected $signature = 'test1';

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
        //
        $output = new ConsoleOutput();
        $progressBar = new ProgressBar($output, 1000);
        $progressBar->setFormat("   %elapsed:6s%/%estimated:-6s%   内存消耗: %memory:6s%\n%current%/%max% [%bar%] %percent:3s%%");

        foreach (range(1, 1000) as $_) {
            usleep(5000);
            $progressBar->advance();
        }

        $progressBar->finish();
        echo "\n";
    }
}

  

效果:

猜你喜欢

转载自www.cnblogs.com/eleven24/p/9134303.html
今日推荐