-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgress.php
More file actions
29 lines (27 loc) · 967 Bytes
/
Progress.php
File metadata and controls
29 lines (27 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
class Progress {
/**
* 终端显示进度条
* @param $percent 进度(百分数)
* @param $particleSize 刷新粒度,默认是1
*/
public static function showProgressBar($percent, $particleSize = 1) {
static $lastPercent = 0;
if ((($percent - $lastPercent) > $particleSize) || $percent >= 100) {
$lastPercent = $percent;
system("tput clear");
echo self::_buildLine($percent) . PHP_EOL;
}
}
private static function _buildLine($percent) {
$done = ($percent > 0) ? str_repeat('■', $percent) : '';
$left = (100 - $percent > 0) ? str_repeat(' ', 100 - $percent) : '';
$buffer = sprintf("[{$done}{$left}]");
if ($percent !== 100) {
$percentString = sprintf("[%-6s]", $percent . '%');
} else {
$percentString = sprintf("[%-5s]", 'OK');;
}
return $percentString . $buffer . PHP_EOL;
}
}