/** * Ascii progress bar function - draws a progress bar * * @param $now number containing what item is beeing processed e.g.: 42 * @param $total number containing the total amount of items e.g.: 101 * @param $text what to put in front of the bar * [@param $width how many chars the bar is in total, keep it under 80] * * @returns void * @author Mads Jørgensen (php mads sulau dk) */ function print_progress($now=null, $total=null, $text="Progress", $width=30) { // where ware we static $drawn = false; static $last_precent = 0; // where are we $precent = ceil($now/$total*100); // check for reset if ($now == null && $total == null || $last_precent > $precent) { $last_precent = 0; $drawn = false; return; } if ($precent > 100) { $precent = 100; } if ($now > $total) { $now = $total; } // how much space do we really have $real_width = $width-strlen($text)-2; // precent width $precent_width = $real_width/100; // draw the bar if (!$drawn) { print $text .": "; print "|"; print str_repeat(".", $real_width+3); print "|"; $drawn = true; } // the fun part if ($last_precent != $precent) { print str_repeat(chr(8), $real_width+4); print str_repeat("-", floor($precent_width*$precent)); if ($precent != 100) { print str_repeat(".", $real_width-floor($precent_width*$precent)-1); } print "| "; print str_pad($precent, 2, " "); print "%"; } // check for done $last_precent = $precent; } for ($i=0; $i<20; $i++) { print_progress($i, 19, "Progress"); usleep(50 * 1000); } print "\n"; print "foo\n"; for ($i=0; $i<10; $i++) { print_progress($i, 10, "Progress"); usleep(50 * 1000); } print "\n";