Skip to content

Instantly share code, notes, and snippets.

@mayconbordin
Created June 2, 2012 23:55
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save mayconbordin/2860547 to your computer and use it in GitHub Desktop.
Save mayconbordin/2860547 to your computer and use it in GitHub Desktop.
PHP CLI progress bar in 5 lines of code
<?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);
}
@ZaneCEO
Copy link

ZaneCEO commented Jun 6, 2018

Amazing, thanks!

@Neries
Copy link

Neries commented Nov 9, 2018

nice and simple! thx!

@stefangrosse
Copy link

Great, thanks!

@xanaDev
Copy link

xanaDev commented Jun 27, 2019

Superb !

@crtl
Copy link

crtl commented Jul 29, 2019

Thanks!

@norahm4d
Copy link

norahm4d commented Sep 2, 2019

Thanks!

@jonhassall
Copy link

Very useful.

@harryqt
Copy link

harryqt commented Oct 13, 2019

amazing.. simple and exactly what I was looking for. thank you so much.

@FLasH3r
Copy link

FLasH3r commented Nov 4, 2019

How can I clear the progress row once the progress is 100% ?

@fernandojmartin
Copy link

8 years later, it worked like a charm.
Thank you

@cikal
Copy link

cikal commented Jan 25, 2020

First comment in 2020, simple snippet..
Thanks

@mklepaczewski
Copy link

25 years of programming and I just realized what's '\r' for...

@vladshut
Copy link

Great! Thanks!

@achraf-jeday
Copy link

It works, thanks.

@fadhil-riyanto
Copy link

Great! Thanks!

@justsomexanda
Copy link

"This will make a fine addition to my collection" - General Grievous

Added this code to my essentials repo, thank you!

@playwithbear
Copy link

Absolutely fantabulous! Thanks

@playwithbear
Copy link

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); }

@hAbd0u
Copy link

hAbd0u commented Mar 6, 2021

Would provide a full example of this snippet?

@Clicketyclick
Copy link

Can't make a pull request, but made an update with more info and a slight reformat at
my gist

@gaiterjones
Copy link

If you want an emoji bar -

	public function emojiPercentBar($done,$total=100)
	{
		$green=html_entity_decode('&#x1F7E9;', 0, 'UTF-8');
		$white=html_entity_decode('&#x2B1C;', 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));

	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment