/**
多进度条输出
*/
ini_set('max_execution_time', '0');
$percentStatus = [];
// 组合成进度条
function buildLine(
repeatTimes = 100;
if (
hasColor = str_repeat('■',
hasColor = '';
}
if ($repeatTimes - $percent > 0) {
$noColor = str_repeat(' ', $repeatTimes - $percent);
} else {
$noColor = '';
}
$buffer = sprintf("[{$hasColor}{$noColor}]");
if ($percent !== 100) {
$percentString = sprintf("[ %-6s]", $percent . '%');
} else {
$percentString = sprintf("[ %-5s]", 'OK');;
}
return $percentString . $buffer . "\r";
}
// 输出进度条
function outputProgress(
percentStatus;
if ($clear) {
$number = count($percentStatus);
for ($i=0; $i < $number; $i++) {
system("tput cuu1");
system("tput el");
}
}
foreach ($percentStatus as $value) {
echo buildLine($value) . "\n";
}
}
// 更新进度条值
function updateProgressValue(
value) {
k] =
percentStatus[
percentStatus[$k] = 100;
outputProgress(true);
return;
}
outputProgress(true);
usleep(50000);
}
percentStatus[1] = 0;
percentStatus[3] = 0;
percentStatus[5] = 0;
percentStatus[7] = 0;
percentStatus[0] = rand(0, 100);
percentStatus[2] = rand(0, 100);
percentStatus[4] = rand(0, 100);
percentStatus[6] = rand(0, 100);
percentStatus[8] = rand(0, 100);
outputProgress(true);
usleep(500000);
}
这段代码展示了如何在命令行中创建一个多进程进度条输出的示例。通过ini_set设置最大执行时间,并使用buildLine函数构建不同颜色的进度条。程序随机生成多个进度值并实时更新显示,提供了一个清除屏幕和输出进度条的功能。
2357

被折叠的 条评论
为什么被折叠?



