Last active
February 19, 2019 12:46
-
-
Save bantya/095f598c626bf85cdcab6c4eb5207fad to your computer and use it in GitHub Desktop.
Php: CLI progress bar examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# https://gist.github.com/nicklasos/7b83682aedf4198be9c0 | |
# Progress: 49% | |
function simple_progress($text, $value) | |
{ | |
echo "\r{$text}: {$value}%"; | |
flush(); | |
} | |
for ($i = 1; $i < 100; $i++) { | |
simple_progress("Progress", $i); | |
usleep(10000); | |
} | |
# https://gist.github.com/mertvetsky/7d04d7e0195b56e3dc61 | |
# [################------------------------------------------------------------------------------------] 16% 16/100 | |
function progressBar($cur = 0, $max = 0, $full = '#', $empty = '-') | |
{ | |
$p = (int)($cur / $max * 100); | |
return $max < 100 || $cur % (int)($max / 100) == 0 || $p == 100 ? | |
sprintf("\r[%s%s] %d%% %d/%d", str_repeat($full, $p), str_repeat($empty, 100 - $p), $p, $cur, $max) : ''; | |
} | |
$max = 100; | |
foreach (range(0, $max) as $i) { | |
echo progressBar($i, $max); | |
usleep(rand(10000, 200000)); | |
} | |
# https://gist.github.com/monkeywire/3849196 | |
# [===========>--------------] 45 % 45 / 100 remaining: 5 sec. elapsed: 4 sec. | |
# @updated 2019-02-19 Added progress chars, format, message etc. | |
function show_status ( | |
$done, | |
$total, | |
$size = 25, | |
$filled = '=', | |
$leading = '>', | |
$empty = '-', | |
$format = '{percentages} % {current} / {total} | remaining: {remaining} sec. elapsed: {passed} sec.', | |
$message = "\n" | |
) { | |
static $start_time; | |
if ($done > $total) { | |
return; | |
} | |
if (empty($start_time)) { | |
$start_time = time(); | |
} | |
$now = time(); | |
$perc = (double) ($done / $total); | |
$bar = floor($perc * $size); | |
$status_bar = "\r["; | |
$status_bar .= str_repeat($filled, $bar); | |
if ($bar < $size) { | |
$status_bar .= $leading; | |
$status_bar .= str_repeat($empty, $size-$bar); | |
} else { | |
$status_bar .= $filled; | |
} | |
$disp = number_format($perc * 100, 0); | |
$rate = ($now - $start_time) / $done; | |
$left = $total - $done; | |
$eta = round($rate * $left, 2); | |
$elapsed = $now - $start_time; | |
$status_bar .= "] "; | |
$replace = ['{percentages}', '{current}', '{total}', '{remaining}', '{elapsed}']; | |
$with = [$disp, $done, $total, number_format($eta), number_format($elapsed)]; | |
$status_bar .= str_replace($replace, $with, $format); | |
echo "$status_bar"; | |
flush(); | |
if ($done == $total) { | |
echo $message; | |
} | |
} | |
for($x = 1; $ x<= 100; $x++){ | |
show_status($x, 100); | |
usleep(100000); | |
} | |
# https://gist.github.com/shaneharter/4211173 | |
# Percent Complete: 46.0% | |
function progress ($step_x, $of_y, $label = '') { | |
$p = number_format($step_x / $of_y * 100, 1) . '%'; | |
$p = str_pad($p, 5, ' ', STR_PAD_LEFT); | |
if ($label) | |
$p = "{$label} {$p}"; | |
echo str_repeat("\010", strlen($p)); | |
if ($step_x < $of_y) | |
echo $p; | |
else | |
echo str_repeat(' ', strlen($p)) . PHP_EOL; | |
} | |
for ($i=1; $i<101; $i++) { | |
progress($i, 100, 'Percent Complete: '); | |
usleep(25000); | |
} | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment