-
-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugins/progress: A better progress bar #135
base: master
Are you sure you want to change the base?
Conversation
Add progress2 and begin improving and adding progress displays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping. -- This has been sitting a while, and was approved a long time ago. Any plans to merge it?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this new file should be placed in a separate directory plugins/progress2
.
################################################################################ | ||
#Usage: | ||
# source into your script. | ||
# progress int n(1...100) string<"Phase"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain the reasoning for changing the header style? If we should follow this style for some reason, should we also update lib/util.sh
, which has a similar header?
|
||
source $OSH/plugins/progress/progress2.plugin.sh | ||
|
||
for i in {0..101}; do progress $i; done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo?
for i in {0..101}; do progress $i; done | |
for i in {0..101}; do progress2 $i; done |
Also, why does the above example finally call progress2 101
?
_COMPLETE=$(printf %${_LEFT_COUNT}s | tr " " $_COMPLETE_CHAR) | ||
_REMAIN=$(printf %${_RIGHT_COUNT}s | tr " " $_REMAIN_CHAR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would like to reduce the number of forks and execs for efficiency reasons. We require Bash 3.2 for oh-my-bash
, so we can use printf -v _COMPLETE ...
, and tr
can be replaced by _COMPLETE=${_COMPLETE// /$_COMPLETE_CHAR}
.
function progress2() { | ||
# Set the initial vars if they haven't been set already. | ||
if ((${#_REMAIN_CHAR} == 0)); then init_progress; fi | ||
_PCT_NOW=$1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_PCT_NOW=$1 | |
local _PCT_NOW=$1 |
Please also declare local
for the other variables whose values do not need to be preserved among the calls of progress2
.
_COMPLETE=$(printf %${_LEFT_COUNT}s | tr " " $_COMPLETE_CHAR) | ||
_REMAIN=$(printf %${_RIGHT_COUNT}s | tr " " $_REMAIN_CHAR) | ||
echo -ne "$_GAUGE_LABEL$_OPEN_GAUGE_CHAR$_COMPLETE$_REMAIN$_CLOSE_GAUGE_CHAR $_OPEN_PCT_CHAR$_PCT_NOW%$_CLOSE_PCT_CHAR\r" | ||
sleep $_STEP_DELAY |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we insert a delay here? The progress bars are used to indicate the progress of an actual task that takes a long time. If we insert a delay here, the processing time will further increase. Or, is progress2
intended to show fake progress bars?
_STEP_DELAY=0.01 | ||
_REMAIN_CHAR="." | ||
_COMPLETE_CHAR="#" | ||
_OPEN_GAUGE_CHAR='[' | ||
_CLOSE_GAUGE_CHAR=']' | ||
_GAUGE_LABEL="Progress:" | ||
_OPEN_PCT_CHAR="(" | ||
_CLOSE_PCT_CHAR=")" | ||
_PCT_THEN=0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are recently thinking of namespacing variables specific to each plugin or alias. Maybe could you rename them to have the form _OMB_PLUGIN_PROGRESS2_*
?
# to customize the display. | ||
function init_progress { | ||
# Make $LINES $COLUMNS available using the builtin magic of shopt! | ||
shopt -s checkwinsize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shopt -s checkwinsize
will not take effect until the first execution of an external command or a subshell command (...)
. For example, we can insert a line [[ ${COLUMNS-} ]] || (true) # force update COLUMNS
after shopt -s checkwinsize
.
Fixed a bug in the existing progress bar plugin where "phase" was misnamed.
Then I couldn't leave well enough alone so I wrote a much better version.
I left the original version with my fix, and named my rewrite progress2. I wasn't sure about just rm'ing it, but my version is backwards compatible with the existing plugin.
To try it out, first source the progress2.plugin.sh script. Then just ...
for i in {0..100}; do progress2 $i; done