Skip to content

Instantly share code, notes, and snippets.

@marcogrueter
Created October 18, 2013 07:38
Show Gist options
  • Save marcogrueter/7037821 to your computer and use it in GitHub Desktop.
Save marcogrueter/7037821 to your computer and use it in GitHub Desktop.
Percentage progress bar on cmd line in php
$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