Last active
March 21, 2016 15:34
-
-
Save mertvetsky/7d04d7e0195b56e3dc61 to your computer and use it in GitHub Desktop.
php-cli progress bar
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 | |
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) : ''; | |
} | |
// demo | |
$max = 54321; | |
foreach (range(0, $max) as $i) { | |
echo progressBar($i, $max); | |
usleep(rand(10, 200)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment