-
-
Save WizardOfArc/4220884 to your computer and use it in GitHub Desktop.
Progress Display for PHP CLI scripts
This file contains hidden or 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
| <?php | |
| /** | |
| * Azi modified Shane Harter's code to draw a progress bar | |
| * feel free to use | |
| **/ | |
| function progress ($step_x, $of_y, $label = '') { | |
| $screen_width = `tput cols` - 2 ; | |
| $fraction_done = $step_x / $of_y; | |
| $percent_done = $fraction_done * 100; | |
| $p = number_format($percent_done, 1) . '%'; | |
| $p = str_pad($p, 5, ' ', STR_PAD_LEFT); | |
| if ($label) | |
| $p = "{$label} {$p}"; | |
| echo "\r"; | |
| $total_bar_length = $screen_width - (3 + strlen($p)); | |
| $done = round($fraction_done * $total_bar_length); // <--- rounding this number stops shakiness | |
| $still_to_go = $total_bar_length - $done; | |
| $da_bar = str_repeat('=', $done); | |
| $da_space = str_repeat(' ', $still_to_go); | |
| if ($step_x < $of_y) | |
| echo "[$da_bar$da_space] $p"; | |
| else | |
| echo str_repeat(' ', $screen_width) . PHP_EOL; | |
| } | |
| for ($i=1; $i<101; $i++) { | |
| progress($i, 100, 'Percent Complete: '); | |
| usleep(25000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment