I am currently trying to add a progress bar to a command line script and I\'ve tried various solutions (including Zend and Console_ProgressBar). The problem they both have i
The following is for unix machines.
The goal is to retrieve the current terminal total columns. (using tput
)
This is a base, ready to be expanded.
#!/usr/bin/php
The ob
actions are here to hide the stdout
of tput
.
Let's say you would like to make a progress bar matching your task progress.
Just divide the remaining time in seconds by the numbers of columns.
You may end up with microseconds, so it's best to use usleep
.
This way the progress bar will always match the user shell width, including when resized.
To do the same thing in pure bash:
for ((i=0; i<$(tput cols); i++)); do echo -e "█\c" ;done
This shows the main singularity of the php echo
: it does not append a new line, while the bash echo
does.
When using loops, say to check for a condition in intervals, one other nice way to warn for running activity is text effects.
The following using strtoupper
and the ansi code reverse video.
#!/usr/bin/php
Output:
Some may likes the party version, obtained by iteration of ansi codes.
#!/usr/bin/php
See more about ANSI codes: https://stackoverflow.com/a/48365998/2494754