Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions plugins/progress/README.md
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.


5 changes: 5 additions & 0 deletions plugins/progress/example.sh
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Suggested change
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?

25 changes: 10 additions & 15 deletions plugins/progress/progress.plugin.sh
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">
Copy link
Contributor

@akinomyoga akinomyoga Dec 25, 2021

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?



#
Expand All @@ -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;
Expand Down
77 changes: 77 additions & 0 deletions plugins/progress/progress2.plugin.sh
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
Copy link
Contributor

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.

_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
Copy link
Contributor

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_*?

}

#
# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_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.

# 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
Copy link
Contributor

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}.

echo -ne "$_GAUGE_LABEL$_OPEN_GAUGE_CHAR$_COMPLETE$_REMAIN$_CLOSE_GAUGE_CHAR $_OPEN_PCT_CHAR$_PCT_NOW%$_CLOSE_PCT_CHAR\r"
sleep $_STEP_DELAY
Copy link
Contributor

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?

fi
fi
_PCT_THEN=$_PCT_NOW
}