-
-
Save mayconbordin/2860547 to your computer and use it in GitHub Desktop.
<?php | |
function progress_bar($done, $total, $info="", $width=50) { | |
$perc = round(($done * 100) / $total); | |
$bar = round(($width * $perc) / 100); | |
return sprintf("%s%%[%s>%s]%s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width-$bar), $info); | |
} |
nice and simple! thx!
Great, thanks!
Superb !
Thanks!
Thanks!
Very useful.
amazing.. simple and exactly what I was looking for. thank you so much.
How can I clear the progress row once the progress is 100% ?
8 years later, it worked like a charm.
Thank you
First comment in 2020, simple snippet..
Thanks
25 years of programming and I just realized what's '\r' for...
Great! Thanks!
It works, thanks.
Great! Thanks!
"This will make a fine addition to my collection" - General Grievous
Added this code to my essentials repo, thank you!
Absolutely fantabulous! Thanks
I've modified it to give you a running count too:
function progressBar($done, $total, $info = "", $width = 50) { $perc = round(($done * 100) / $total); $bar = round(($width * $perc) / 100); return sprintf("%s%%[%s>%s] %s/%s %s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width - $bar), $done, $total, $info); }
Would provide a full example of this snippet?
Can't make a pull request, but made an update with more info and a slight reformat at
my gist
If you want an emoji bar -
public function emojiPercentBar($done,$total=100)
{
$green=html_entity_decode('🟩', 0, 'UTF-8');
$white=html_entity_decode('⬜', 0, 'UTF-8');
$perc = round(($done * 100) / $total);
$bar = round((10 * $perc) / 100);
return sprintf("%s%s", str_repeat($green, $bar), str_repeat($white, 10-$bar));
}
I think it's more consistent to use floor
instead of round
. And why return
, simply echo the output.
function progress_bar($done, $total, $info="", $width=50) {
$perc = floor(($done * 100) / $total);
$bar = floor(($width * $perc) / 100);
echo sprintf("%s%%[%s>%s] %s/%s %s\r", $perc, str_repeat("=", $bar), str_repeat(" ", $width - $bar), $done, $total, $info);
}
Amazing, thanks!