Skip to content

Instantly share code, notes, and snippets.

@WayneSan
Last active December 17, 2015 02:48
Show Gist options
  • Save WayneSan/5538108 to your computer and use it in GitHub Desktop.
Save WayneSan/5538108 to your computer and use it in GitHub Desktop.
#!/bin/bash
#### Preprocessing ####
if [ "$1" == "--color" ]; then COLOR="TRUE"; shift; fi
if [ $# -lt 2 ]; then
echo "Usage: $0 [--color] TITLE PROGRESS [MAXIMUM]"
exit
fi
if [ $# -ge 3 ] && [ $3 -gt 0 ]; then MAX=$3; else MAX=100; fi
PRG=$2
if [ $PRG -gt $MAX ]; then
echo "Error: PROGRESS needs smaller than MAXIMUM."
exit
fi
#### Main Program ####
echo -en '\r'
echo -n "$1: "
LI=('\' '|' '/' '-')
let MAXL=`echo $MAX | wc -m`-1
let COLS=`tput cols`-`echo $1 | wc -m`-$((MAXL * 2))-23
let PEC=(PRG*100)/MAX
echo -n '[ '
PRGBAR=`printf '%*s' $((PEC * COLS / 100)) | tr ' ' '#'`
[ "$COLOR" == "TRUE" ] && echo -en "\033[1;3$(($PRG % 8))m"
printf "%-${COLS}.${COLS}s" "$PRGBAR${LI[$PRG % 4]}"
[ "$COLOR" == "TRUE" ] && echo -en "\033[m"
#echo -n " ] $PEC%"
#echo -n " ($PRG/$MAX)... "
printf " ] %3s%% (%${MAXL}s/%${MAXL}s)... " "$PEC" "$PRG" "$MAX"
[ $PRG -ge $MAX ] && echo 'done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment