guiguiboy/php-cli-progress-bar
最新稳定版本:0.0.4
Composer 安装命令:
composer require guiguiboy/php-cli-progress-bar
包简介
Progress bar for PHP CLI scripts
关键字:
README 文档
README
A PHP5 CLI Progress bar Version 0.0.4
Requirements
PHP >= 5.3
How it works
There is one namespace ProgressBar that contains 2 classes Manager and Registry.
Manager is responsible to manage the progress bar. Each instance of this class is associated with a Registry object. Each time the Manager needs to keep a metric, it is stored in this object.
When the display is requested, the script uses the string format and iterates over all replacement rules. Replacements are handled by closures.
The progress bar has the following default output : %current%/%max% [%bar%] %percent%% %eta%
It is configurable. You can also change it while processing your batch script.
Buit-in variable replacement are :
- %current% : the current element
- %max% : the number of elements
- %bar% : the progress bar
- %percent% : the advancement in percent
- %eta% : estimation of the remaining
Manager constructor arguments :
- current : the initial step
- max : the amount of steps in your process
- width : the max width of the line (default : 80)
- doneBarElementCharacter : a character to identify done advancement in the progress bar (default : =)
- remainingBarElementCharacter : a character to identify remaining advancement in the progress bar (default : -)
- currentPositionCharacter : a character to identify the current position in the progress bar (default : >)
How to use
Quick start
Add include statements at the beginning of your script (if you don't have autoloaders)
<?php require_once 'ProgressBar/Manager.php'; require_once 'ProgressBar/Registry.php'; $progressBar = new \ProgressBar\Manager(0, 10); for ($i = 0; $i <= 10; $i++) { $progressBar->update($i); sleep(1); }
Will output :
1/10 [===>----------------------------------------------] 10.00% 00:00:09
When you want to iterate over a collection, you don't event need to track the counter:
foreach ($array as $element) { // process element $progressBar->advance(); }
Configuration
Changing the output
Use the setFormat() method :
$progressBar->setFormat('%current% |%bar%| %max%'); $progressBar->update(1);
Will output :
1|>-------------------------------------------------------------------| 10
Changing the max length
The max length is specified in the constructor :
$pb = new \ProgressBar\Manager(0, 20, 120); $progressBar->update(1);
Will output :
1/20 [====>----------------------------------------------------------------------------------------] 5.00% 00:00:00
Changing the progress bar style
Use the parameters specified in the constructor :
$pb = new \ProgressBar\Manager(0, 20, 120, '-', ' ', ')'); $pb->update(5);
Will output :
5/20 [-----------------------) ] 25.00% 00:00:00
Extending
Adding custom replacement rules
You may add your custom variables for replacement. You have to use the method addReplacementRule and specify a priority, a tag and a closure. Keep in mind that the length of the progress bar is evaluated at the end so that the output width will scale up to the width you specified. So keeping the %bar% with a high priority is a good practice.
Here is an example of what you should do if you want to add a new replacement rule.
<?php use ProgresBar; $pb = new Manager(0, 213); $pb->setFormat('Progress : %current%/%max% [%bar%] %foo%'); $pb->addReplacementRule('%foo%', 70, function ($buffer, $registry) {return 'OK!';}); $pb->update(1);
Will echo :
Progress : 1/213 [>---------------------------------------------------] OK!
ChangeLog
0.0.3 -> 0.0.4
- Changed RuntimeExceptions to InvalidArgumentExceptions
- Added advance() method for incrementing the progress bar
- Forbid to set the progress value greater than expected
0.0.2 -> 0.0.3
- Added composer support
- Added unit tests for the Manager and the registry
0.0.1 -> 0.0.2
- Changed directory structure to add namespace
- Changed priority behavior
- Added new public method addReplacementRule so that you don't need to extend the manager to add custom replacement rules
TODO
- ask developpers for feedback
guiguiboy/php-cli-progress-bar 适用场景与选型建议
guiguiboy/php-cli-progress-bar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 577.49k 次下载、GitHub Stars 达 158, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cli」 「command-line」 「progress」 「bar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 guiguiboy/php-cli-progress-bar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guiguiboy/php-cli-progress-bar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 guiguiboy/php-cli-progress-bar 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PACE (Progress Automatically Certain to Entertain) - Automatic page load progress bar for Yii PHP Framework
A help command for Minicli
Reusable progress bar components for Filament 5 tables and infolists.
Command-line utility for Vtiger CRM.
Rules for the bdd-analyser CLI tool
Use any command-line tool as a PHP function.
统计信息
- 总下载量: 577.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 165
- 点击次数: 23
- 依赖项目数: 15
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 未知