Discuss PHP CLI Progress Indicator in the PHP Development forum on Dev Shed. PHP CLI Progress Indicator PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
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";
}
}
Posts: 2
Time spent in forums: 13 m 13 sec
Reputation Power: 0
Not bad, but I've improved it.
PHP Code:
function progressBar($current=0, $total=100, $size=50)
{
// first call must have $current=0,
// otherwise you'll delete some last
// part of your's app output
// percent indicator must be four characters, if shorter, add some spaces
$perc = ($current/$total)*100;
for($i=strlen($perc); $i<=4; $i++)
$perc = ' '.$perc;
$total_size = $size + $i + 3;
// if it's not first go, remove the previous bar
if($current > 0)
{
for($place = $total_size; $place > 0; $place--)
{
// echo a backspace (hex:08) to remove the previous character
echo "\x08";
}
}
// output the progess bar as it should be
for($place = 0; $place <= $size; $place++)
{
// output green spaces if we're finished through this point
// or grey spaces if not
if($place <= ($current / $total * $size))
echo '[42m [0m';
else
echo '[47m [0m';
}
// end a bar with a percent indicator
echo " $perc%";
if($current == $total)
{
// if it's the end, add a new line
echo "\n";
}
}
Posts: 1
Time spent in forums: 1 m 54 sec
Reputation Power: 0
Another update
I know this is a fairly old thread but someone might stumble upon it as I have recently. Here is another version of mikiroro's bar with further improvments.
PHP Code:
function progressBar($current=0, $total=100, $label="", $size=50) {
//Don't have to call $current=0
//Bar status is stored between calls
static $bars;
if(!isset($bars[$label])) {
$new_bar = TRUE;
fputs(STDOUT,"$label Progress:\n");
}
if($current == $bars[$label]) return 0;
$perc = round(($current/$total)*100,2); //Percentage round off for a more clean, consistent look
for($i=strlen($perc); $i<=4; $i++) $perc = ' '.$perc; // percent indicator must be four characters, if shorter, add some spaces
$total_size = $size + $i + 3;
// if it's not first go, remove the previous bar
if(!$new_bar) {
for($place = $total_size; $place > 0; $place--) echo "\x08"; // echo a backspace (hex:08) to remove the previous character
}
$bars[$label]=$current; //saves bar status for next call
// output the progess bar as it should be
for($place = 0; $place <= $size; $place++) {
if($place <= ($current / $total * $size)) echo '[42m [0m'; // output green spaces if we're finished through this point
else echo '[47m [0m'; // or grey spaces if not
}
// end a bar with a percent indicator
echo " $perc%";
if($current == $total) {
echo "\n"; // if it's the end, add a new line
unset($bars[$label]);
}
}