Created
October 18, 2013 07:38
-
-
Save marcogrueter/7037821 to your computer and use it in GitHub Desktop.
Percentage progress bar on cmd line in php
This file contains 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
$data = getData(); | |
$step = 100 / count($data); | |
$percent = 0; | |
echo 'Doing stuff with data: '; //padding at the end for the number | |
foreach($data as $row) | |
{ | |
// do stuff | |
//then | |
$percent += $step; | |
if($percent < 10) // for single digit output | |
{ | |
echo "\033[5D"; | |
} | |
else if($percent > 10 && $percent < 100) // two digits | |
{ | |
echo "\033[6D"; | |
} | |
else | |
{ | |
echo "\033[7D"; // and finally, three | |
} | |
// nice format with stuff and padding | |
echo str_pad( number_format($percent, 2), 3, ' ', STR_PAD_LEFT) . "%"; | |
// flush the output | |
// use ob_flush() or flush(), depending on your situation | |
ob_flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment