alex-heifetz/progress-bar

PHP CLI 进度条

1.3.2 2024-08-24 19:35 UTC

This package is auto-updated.

Last update: 2024-09-08 12:22:00 UTC


README

GitHub Release GitHub Downloads (all assets, all releases) GitHub License Packagist Dependency Version

Gif

功能

  • 兼容 PHP 7.4 及以上版本
  • 显示预估完成时间
  • 可以分成两部分,例如成功的迭代和错误的迭代
  • 灵活响应窗口宽度变化
  • 可以配置数据更新时间

你可能为什么需要这个

有时在运行命令和长时间处理或测试命令时,你希望看到进程正在前进,而不是停留在中间。我还想了解进程的大致完成时间。这个库就是为了解决这些问题而创建的。

安装与加载

只需将此行添加到你的 composer.json 文件中

"alex-heifetz/progress-bar": "^1.2"

或者运行

composer require seraph90/progress-bar

简单示例

<?php

// Import ProgressBar class into the global namespace
use Heifetz\ProgressBar;

// Load Composer's autoloader
require 'vendor/autoload.php';

$count = 12000000;

// Create an instance;
// Passing total count of iterations 
$pb = new ProgressBar($count);

// Set the delay time between redrawings
$pb->setRenderDelay(100);

for ($i = 0; $i < $count; $i++) {
    if (mt_rand(0, 5)) {
        // Advance the process by one iteration
        $pb->advance();
    } else {
        // Advance the process by one iteration with error
        $pb->advanceError();
    }
}

// We indicate the completion of the progress bar to render the final state
$pb->finish();

ProgresBar