macroman/terminal-progress-bar

灵活的ASCII进度条。

0.1.8 2023-05-27 21:27 UTC

This package is auto-updated.

Last update: 2024-09-28 00:12:33 UTC


README

灵活的ASCII进度条。

安装

composer require macroman/terminal-progress-bar

使用

首先我们创建一个 Bar,给它一个 format 字符串以及 total,告诉进度条何时完成。然后根据需要调用 tick()

// examples/basic.php
use TerminalProgress\Bar;

$pg = new Bar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	$pg->tick();
}

您还可以使用 update(amount) 来设置当前的 tick 值,而不是每次增加时都进行 tick。

// examples/update.php
use TerminalProgress\Bar;

$pg = new Bar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	$pg->update($i);
}

选项

这些是您可以读取/设置的对象属性

  • symbolComplete 完成字符默认为 "="
  • symbolIncomplete 不完整字符默认为 " "
  • throttle 更新之间的最小时间(秒),默认为 0.016
  • current 当前 tick
  • total 初始化时传入的相同值
  • secondPrecision 在 "seconds" 单位中使用的小数位数
  • percentPrecision 在 "percentage" 单位中使用的小数位数
  • percent(只读)当前完成百分比
  • eta(只读)估计完成所需时间(秒)
  • rate(只读)每秒的 tick 数
  • elapsed(只读)自初始化以来的秒数

令牌

您可以在进度条格式中使用以下令牌。

  • :bar 进度条本身
  • :current 当前 tick 号码
  • :total 总 tick 数
  • :elapsed 已经过去的秒数
  • :percent 完成百分比
  • :eta 预计完成时间(秒)
  • :rate 每秒的 tick 数

格式示例

// examples/format.php
// Full options
new Bar(10, "Progress: [:bar] - :current/:total - :percent% - Elapsed::elapseds - ETA::etas - Rate::rate/s");
// examples/format_percent.php
// Just percentage plus the bar
new Bar(10, ":bar :percent%");
// examples/format_no_bar.php
// You don't even have to have a bar
new Bar(10, "Look mum, no bar! :current/:total - :percent% - Elapsed::elapseds - ETA::etas - Rate::rate/s");

中断示例

要在进度条执行期间显示消息,请使用 interrupt()

// examples/interrupt.php
$pg = new Bar(1000);

for ($i = 0; $i < 1000; $i++) {
	usleep(10000);
	if ($i % 100 == 0) {
		// Interupt every 100th tick
		$pg->interupt($i);
	}
	$pg->tick();
}

符号/精度示例

要更改进度条上使用的符号或精度

// examples/symbols.php
$pg = new Bar(1000);

$pg->symbolComplete = "#";
$pg->symbolIncomplete = "-";

$pg->secondPrecision = 2;
$pg->percentPrecision = 4;

节流示例

绘制间隔被节流在每 100ms 一次以提高性能。如果需要,您可以更改此值,例如,如果动画更平滑则降低,如果工作更消耗资源则提高。

// examples/throttle.php
$pg = new Bar(1000);
$pg->throttle = 0.05; // Set a 50 millisecond throttle

许可

查看 LICENSE