
May 27th, 2004, 10:20 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: Oxford, MS USA
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
PHP CLI Progress Indicator
I don't know if this is useful to anyone else, but I saw on the Perl forums someone else was looking for a solution to this . I finally had to break out my ASCII tables to figure out \010 is the backspace character I needed.
Anyway, if anyone is interested, I've written a simple progess bar for PHP command line interface (CLI) scripts. I've posted it, including comments, below.
PHP Code:
function progressBar($current, $total, $label)
{
// This function assumes that you start with completion of 0%.
// If the first time you call this function is with 1%
// completion, you will delete the last 106 characters of
// output from your program.
// If this is the case, simply call this function before with
// a hard coded 0.
// check to see if this is the first go-round
if ($current == 0)
{
// this is the first time so output the progess bar label
if ($label == "")
echo "Progress: ";
else if ($label != "none")
echo $label;
// start the bar with a nice edge
echo "|";
}
else
{
// this isn't the first time so remove the previous progress bar
for ($place = 106; $place >= 0; $place--)
{
// echo a backspace to remove the previous character
echo "\010";
}
}
// output the progess bar as it should be
for ($place = 0; $place <= 100; $place++)
{
// output stars if we're finished through this point
// or spaces if not
if ($place <= ($current / $total * 100))
echo "*";
else
echo " ";
}
// end the bar with a nice edge and a label
echo "| 100%";
// check to see if this is the last go-round
if ($current == $total)
{
// this is the end of the progress bar, output an end of line
echo "\n";
}
}
|