-
-
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?
Changes from all commits
324d3e3
2208c24
68c6c74
3914627
3371f91
5a321cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# progress2 | ||
|
||
Spice up your scripts with a full width animated progress bar which also adjusts to window size changes. | ||
|
||
## Usage | ||
|
||
```shell | ||
source $OSH/plugins/progress/progress2.plugin.sh | ||
|
||
init_progress | ||
progress 1 | ||
... | ||
progress 10 | ||
... | ||
progress 90 | ||
... | ||
progress 100 | ||
``` | ||
|
||
## Advanced | ||
|
||
The characters used to draw the progress bar can be changed to suit your tastes. Simply set your own values (see script for complete list) after calling init_progress but before calling progress for the first time. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
source $OSH/plugins/progress/progress2.plugin.sh | ||
|
||
for i in {0..101}; do progress $i; done | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
#!/usr/bin/env bash | ||
############################---Description---################################### | ||
# # | ||
# Summary : Show a progress bar GUI on terminal platform # | ||
# Support : [email protected] # | ||
# Created date : Aug 12,2014 # | ||
# Latest Modified date : Aug 13,2014 # | ||
# # | ||
################################################################################ | ||
#Description: | ||
|
||
############################---Usage---######################################### | ||
# Summary : Show a progress bar GUI on terminal platform | ||
# Support : [email protected] | ||
# Created date : Aug 12,2014 | ||
# Last Modified : Feb, 21, 2020 | ||
# Last Modifier : FG Robertson (https://github.com/grobertson) | ||
|
||
# Copy below functions (delay and progress fuctions) into your shell script directly | ||
# Then invoke progress function to show progress bar | ||
|
||
# In other way, you could import source indirectly then using. Nothing different | ||
|
||
################################################################################ | ||
#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 commentThe 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 |
||
|
||
|
||
# | ||
|
@@ -33,7 +28,7 @@ CURRENT_PROGRESS=0 | |
function progress() | ||
{ | ||
PARAM_PROGRESS=$1; | ||
PARAM_STATUS=$2; | ||
PARAM_PHASE=$2; | ||
|
||
if [ $CURRENT_PROGRESS -le 0 -a $PARAM_PROGRESS -ge 0 ] ; then echo -ne "[..........................] (0%) $PARAM_PHASE \r" ; delay; fi; | ||
if [ $CURRENT_PROGRESS -le 5 -a $PARAM_PROGRESS -ge 5 ] ; then echo -ne "[#.........................] (5%) $PARAM_PHASE \r" ; delay; fi; | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||
#!/usr/bin/env bash | ||||||
|
||||||
# Copyright (c) 2020, F. Grant Robertson, https://github.com/grobertson/ | ||||||
# All rights reserved. | ||||||
# | ||||||
# Redistribution and use in source and binary forms, with or without modification, are permitted provided | ||||||
# that the following conditions are met: | ||||||
# | ||||||
# * Redistributions of source code must retain the above copyright notice, this list of conditions | ||||||
# and the following disclaimer. | ||||||
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||||||
# following disclaimer in the documentation and/or other materials provided with the distribution. | ||||||
# * Neither the name of F. Grant Robertson (aka @grobertson) nor the names of contributors | ||||||
# may be used to endorse or promote products derived from this software without | ||||||
# specific prior written permission. | ||||||
# | ||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | ||||||
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||||||
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | ||||||
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||||||
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||||||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||
# POSSIBILITY OF SUCH DAMAGE. | ||||||
# | ||||||
# Intended to improve upon progress.plugin.sh by [email protected], Aug 13,2014. YMMV. | ||||||
|
||||||
# USAGE: | ||||||
|
||||||
# See README.md | ||||||
|
||||||
# | ||||||
# Description: Called automatically if _REMAIN_CHAR is unset | ||||||
# Call init_progress manually and then set your own values | ||||||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
_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 | ||||||
Comment on lines
+39
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
} | ||||||
|
||||||
# | ||||||
# Description : Call with int {1..100} to display progress | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please also declare |
||||||
# Stubbornly refusing to go backwards. BUT redraw if equal to the last draw anyway | ||||||
# this could be handy if triggering a call to progress on window resize | ||||||
if ((_PCT_NOW > _PCT_THEN)); then | ||||||
if ((_PCT_NOW >= 100)); then | ||||||
#blank the line before \returning to pos 1 to print "Done!" | ||||||
printf %${COLUMNS}s | ||||||
echo -e "\rDone!" | ||||||
else | ||||||
#Recompute everything every time because the shopt will update $COLUMNS | ||||||
# when the term size changes, and the progress bar will adjust itself! | ||||||
_FIELD_COLS=$((COLUMNS - (${#_GAUGE_LABEL} + ${#_OPEN_GAUGE_CHAR} + ${#_CLOSE_GAUGE_CHAR} + ${#_OPEN_PCT_CHAR} + ${#_CLOSE_PCT_CHAR} + 4))) | ||||||
# Bash doesn't 'do' floating point math and using "bc" is cheating | ||||||
_LEFT_COUNT=$((_FIELD_COLS * _PCT_NOW / 100)) | ||||||
_RIGHT_COUNT=$((_FIELD_COLS - _LEFT_COUNT)) | ||||||
_COMPLETE=$(printf %${_LEFT_COUNT}s | tr " " $_COMPLETE_CHAR) | ||||||
_REMAIN=$(printf %${_RIGHT_COUNT}s | tr " " $_REMAIN_CHAR) | ||||||
Comment on lines
+70
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
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 commentThe 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 |
||||||
fi | ||||||
fi | ||||||
_PCT_THEN=$_PCT_NOW | ||||||
} |
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?
Also, why does the above example finally call
progress2 101
?