Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####################################################################################################################
- # progress v3.7.7 - draws progress bar & information in a shell console according to the given value. #
- # Handshake: #
- # $PROGRESS_L1 contains character used to fill the left part of the upper bar (default: "█"). #
- # $PROGRESS_R1 contains character used to fill the right part of the upper bar (default: "_"). #
- # $PROGRESS_L2 contains character used to fill the left part of the lower bar (default: "▓"). #
- # $PROGRESS_R2 contains character used to fill the right part of the lower bar (default: "▒"). #
- # $PROGRESS_I contains character(s) drawn at the beginning of the bar (default: unset). #
- # $PROGRESS_O contains character(s) drawn at the end of the bar (default: unset). #
- # $PROGRESS_P contains the percentage value. Make it empty/unset to turn it off (default: 0/on). #
- # $PROGRESS_FIX contains the fixed width of the progress bar (default: unset). #
- # $PROGRESS_ML contains left margin of the bar (default: 0). #
- # $PROGRESS_MR contains right margin of the bar (default: 0). #
- # $PROGRESS_MAX1 contains the maximum of the value to be processed for the upper bar (default: 100). #
- # $PROGRESS_MAX2 contains the maximum of the value to be processed for the lower bar (default: 100). #
- # $PROGRESS_FT sets the frame type; S for single line, D for double line, F for fat frame, T for twin bars, N for #
- # a narrow framed bar. Anything else will turn off frame design and draw a simple bar instead (default). #
- # $PROGRESS_TSHW sets which time information is displayed: E for elapsed time, R for remaining time, A for all #
- # these infos (default). Anything else disables the timer display. #
- # $PROGRESS_TPOS sets the position of the timer display: T for top (default), M for middle, B for bottom of the #
- # progress bar. Anything else disables the timer display. #
- # PROGRESS_TC contains the number of iterations to calculate the average total time. A higher value might be more #
- # precise, but will also take longer to calculate. (default: 10). #
- # Not using any of these means the corresponding default value is used. #
- # Right margin is ignored if $PROGRESS_FIX is set. #
- # PROGRESS_BAR() must be called with at least a number between 0 & $PROGRESS_MAX1 which is the progress displayed #
- # by the bar. For twin bars a 2nd value between 0 & $PROGRESS_MAX2 must be given. #
- #####################################################################################################################
- PROGRESS_L1=█
- PROGRESS_R1=_
- PROGRESS_L2="\033[38;2;130;130;130m█\033[0;0m"
- PROGRESS_R2="\033[38;2;65;65;65m█\033[0;0m"
- PROGRESS_I=
- PROGRESS_O=
- PROGRESS_P=0
- PROGRESS_ML=0
- PROGRESS_MR=0
- PROGRESS_MAX1=100
- PROGRESS_MAX2=100
- PROGRESS_FT=X
- PROGRESS_TOLD=
- PROGRESS_TSHW=A
- PROGRESS_TPOS=T
- PROGRESS_MPOS=M
- PROGRESS_TAVR=0
- PROGRESS_TREM=---:--:--
- PROGRESS_TTOT=0
- PROGRESS_TC=10
- PROGRESS_FIN=
- PROGRESS_STAGE=1
- # PROGRESS_PREP() - draws top, middle & bottom part of the frame & inserts timer display and/or message text if needed.
- # IN:
- # $1 contains the token for top (T), middle (M) or bottom (B) part of the frame. It will be compared to $PROGRESS_TPOS
- # and $PROGRESS_MPOS to decide whether the timer display and message is included or not.
- # $PROGRESS_ML contains the left margin.
- # OUT:
- # Draws directly to stdout.
- PROGRESS_PREP(){
- PROGRESS_FRAME=$(PROGRESS_DRAW 0)
- [ $1 = $PROGRESS_TPOS ] && PROGRESS_FRAME=$PROGRESS_FRAME$(printf "\033["$(($PROGRESS_ML+2))"C$PROGRESS_TOUT")
- [ $1 = $PROGRESS_MPOS ] && PROGRESS_FRAME=$PROGRESS_FRAME$(printf "\033["$(($PROGRESS_ML+2))"C$PROGRESS_MSG")
- printf "$PROGRESS_FRAME"
- }
- # PROGRESS_DRAW() - does the actual positioning & drawing of the progress bar.
- # IN:
- # $1 must contain the actual progress given by a number between 0 and $PROGRESS_MAX.
- # $PROGRESS_ML contains the left margin.
- # $PROGRESS_L1 & $PROGRESS_L2 contain characters used to fill the left part of the corresponding bar.
- # $PROGRESS_R1 & $PROGRESS_R2 contain characters used to fill the right part of the corresponding bar.
- # $PROGRESS_I contains character(s) drawn at the beginning of the bar.
- # $PROGRESS_O contains character(s) drawn at the end of the bar.
- # $PROGRESS_MAX1 & $PROGRESS_MAX2 contain the maximum of the value for the corresponding bar to be processed.
- # OUT:
- # Draws directly to stdout.
- PROGRESS_DRAW(){
- PROGRESS_COUNT=0
- if [ $PROGRESS_STAGE -eq 1 ]; then
- PROGRESS_L=$PROGRESS_L1
- PROGRESS_R=$PROGRESS_R1
- PROGRESS_MAX=$PROGRESS_MAX1
- else
- PROGRESS_L=$PROGRESS_L2
- PROGRESS_R=$PROGRESS_R2
- PROGRESS_MAX=$PROGRESS_MAX2
- fi
- setterm -linewrap off
- # If left margin was set move the start position to the right according to $PROGRESS_ML.
- if [ $PROGRESS_ML -gt 0 ]; then
- PROGRESS_I_TMP="\033["$PROGRESS_ML"C"$PROGRESS_I
- else
- PROGRESS_I_TMP="$PROGRESS_I"
- fi
- printf "\r$PROGRESS_I_TMP"
- # Draw left part.
- while [ $((PROGRESS_COUNT=$PROGRESS_COUNT+1)) -lt $(($PROGRESS_TOT*$1/$PROGRESS_MAX)) ]; do
- printf %.1s && printf "$PROGRESS_L"
- done
- # Draw the right part.
- while [ $((PROGRESS_COUNT=$PROGRESS_COUNT+1)) -le $PROGRESS_TOT ]; do
- printf %.1s && printf "$PROGRESS_R"
- done
- # Finish drawing, print percentage & clear line.
- printf "$PROGRESS_O%4s" $PROGRESS_P && printf "\033[K\r"
- setterm -linewrap on
- }
- # PROGRESS_CALC() - measures window size; calculates & sets width of the progress bar.
- # IN:
- # $PROGRESS_FIX contains the fixed width of the progress bar. If not set, the width is calculated according to
- # the window size.
- # $PROGRESS_MR contains right margin of the bar.
- # $PROGRESS_EXT contains the length extension for the bar; needed to calculate the actual length without $PROGRESS_I
- # and $PROGRESS_O.
- # OUT:
- # $PROGRESS_TOT contains the total width of the progress bar.
- # $PROGRESS_P contains the percentage value displayed at the end of the progress bar.
- PROGRESS_CALC(){
- PROGRESS_MAX=$PROGRESS_MAX1
- [ $PROGRESS_STAGE -eq 2 ] && PROGRESS_MAX=$PROGRESS_MAX2
- # Fixed width used?
- if [ -n "$PROGRESS_FIX" ]; then
- PROGRESS_MR=0
- PROGRESS_TOT=$PROGRESS_FIX
- else
- # If not, calculate width in relation to window size.
- PROGRESS_TOT=$(($(stty size | awk '{print $2}')-$PROGRESS_ML-$PROGRESS_MR-$PROGRESS_EXT))
- if [ -n "$PROGRESS_P" ]; then
- PROGRESS_TOT=$(($PROGRESS_TOT-4))
- PROGRESS_P=$((100*$1/$PROGRESS_MAX))%
- fi
- fi
- }
- # PROGRESS_TISO() - changes time value into ISO pattern or interger. Given value must be either one of them.
- # IN:
- # $1 must contain the time value. If it's integer it will be converted into ISO pattern and vice versa. For the ISO
- # pattern use the input format hours:minutes:seconds:milliseconds. The output format, however, will always be
- # hours:minutes:seconds.
- # OUT:
- # Prints the corresponding value directly to stdout.
- PROGRESS_TISO(){
- # If input contains anything else but numbers it's considered as ISO pattern & converted into integer.
- if [ -z ${1##*[!0-9+-]*} ]; then
- printf "$1" | awk -F ':' '{print ($1*1000)+$2}'
- else
- PROGRESS_TIN=$1
- [ $PROGRESS_TIN -lt 0 ] && PROGRESS_TIN=0
- PROGRESS_THRS=$(($PROGRESS_TIN/3600000))
- PROGRESS_TMIN=$((($PROGRESS_TIN-$PROGRESS_THRS*3600000)/60000))
- PROGRESS_TSEC=$((($PROGRESS_TIN-($PROGRESS_THRS*3600000)-($PROGRESS_TMIN*60000))/1000))
- printf "%03d" $PROGRESS_THRS
- printf ":%02d" $PROGRESS_TMIN
- printf ":%02d" $PROGRESS_TSEC
- fi
- }
- # PROGRESS_TIME() - counts passing time, calculates remaining time & builds timer display.
- # IN:
- # $1 must contain the actual progress given by a number between 0 and $PROGRESS_MAX.
- # $PROGRESS_TSHW contains the token which sets what the timer display will show. E for elapsed time, R for remaining
- # time, A for both. Anything else will deactivate the timer display.
- # PROGRESS_TC contains the number of iterations to calculate the average total time. A higher value might be more
- # precise, but will also take longer to calculate.
- # OUT:
- # $PROGRESS_TOUT contains the ready-to-use timer display according to the $PROGRESS_TSHW setting.
- # $PROGRESS_TPAS contains the elapsed time.
- # $PROGRESS_TREM contains the remaining time.
- # $PROGRESS_TAVR contains the estimated total time.
- PROGRESS_TIME(){
- PROGRESS_MAX=$PROGRESS_MAX1
- [ $PROGRESS_STAGE -eq 2 ] && PROGRESS_MAX=$PROGRESS_MAX2
- [ -z $PROGRESS_TC_TMP ] && PROGRESS_TC_TMP=$(($PROGRESS_TC*1000))
- PROGRESS_T=$(date +%s:%3N)
- [ -z $PROGRESS_TOLD ] && PROGRESS_TOLD=$PROGRESS_T
- PROGRESS_TINT=$(($(PROGRESS_TISO $PROGRESS_T)-$(PROGRESS_TISO $PROGRESS_TOLD)))
- PROGRESS_TPAS=$(PROGRESS_TISO $PROGRESS_TINT)
- [ $1 -gt 0 ] && PROGRESS_TTOT=$((($PROGRESS_MAX*1000+$1/2)/$1*$PROGRESS_TINT))
- if [ $((PROGRESS_TC=$PROGRESS_TC-1)) -ge 0 ]; then
- PROGRESS_TAVR=$(($PROGRESS_TAVR+$PROGRESS_TTOT))
- else
- PROGRESS_TREM=$((($PROGRESS_TAVR+$PROGRESS_TC_TMP/2)/$PROGRESS_TC_TMP-$PROGRESS_TINT))
- [ $PROGRESS_TREM -lt 0 ] && PROGRESS_TREM=0
- PROGRESS_TREM=$(PROGRESS_TISO $PROGRESS_TREM)
- PROGRESS_TAVR=0
- PROGRESS_TC=$(($PROGRESS_TC_TMP/1000))
- fi
- case $PROGRESS_TSHW in
- E)
- PROGRESS_TOUT=" Time elapsed: $PROGRESS_TPAS "
- ;;
- R)
- PROGRESS_TOUT=" Time remaining: $PROGRESS_TREM "
- ;;
- A)
- PROGRESS_TOUT=" Time elapsed: $PROGRESS_TPAS, remaining: $PROGRESS_TREM "
- ;;
- *)
- PROGRESS_TPOS=X
- ;;
- esac
- }
- # PROGRESS_BAR() - main part; uses all functions to build a progress bar according to the user settings; contains
- # presets for 3 different frame types. Call this function for the most complete progress output.
- # IN:
- # $1 must contain the actual progress given by a number between 0 and $PROGRESS_MAX.
- # $PROGRESS_MAX1 & $PROGRESS_MAX2 contain the maximum of the value for the corresponding bar to be processed.
- # $PROGRESS_FT contains a token to set the frame type: S for single line, D for double line, F for fat frame, T for
- # twin bars, N for a narrow framed bar. Anything else will turn off frame design and draw a simple bar instead.
- # $PROGRESS_ML contains the left margin.
- # $PROGRESS_MR contains the right margin.
- # $PROGRESS_L1 & $PROGRESS_L2 contain characters used to fill the left part of the corresponding bar.
- # $PROGRESS_R1 & $PROGRESS_R2 contain characters used to fill the right part of the corresponding bar.
- # $PROGRESS_I contains character(s) drawn at the beginning of the bar.
- # $PROGRESS_O contains character(s) drawn at the end of the bar.
- # $PROGRESS_P contains the percentage value. Make it empty/unset to turn it off.
- # $PROGRESS_FIN contains an ANSI code which has to be placed at the end of a line in some cases.
- # OUT:
- # $PROGRESS_NLIN contains the number of lines from the entry point to the line where the progress bar is drawn.
- # This is just for orientation in case you want to place something next to the progress bar.
- # Simply call PROGRESS_BAR without argument to obtain the value for the corresponding frame type.
- # Anything else is drawn directly to stdout.
- PROGRESS_BAR(){
- case $PROGRESS_FT in
- S)
- PROGRESS_NLIN=2
- ;;
- D)
- PROGRESS_NLIN=2
- ;;
- F)
- PROGRESS_NLIN=3
- ;;
- T)
- PROGRESS_NLIN=2
- ;;
- N)
- PROGRESS_NLIN=2
- ;;
- esac
- # Is input integer & between 0 & $PROGRESS_MAX1?
- [ -z ${1##*[!0-9]*} ] || [ $1 -lt 0 ] || [ $1 -gt $PROGRESS_MAX1 ] && return 1
- PROGRESS_EXT=$((${#PROGRESS_I}+${#PROGRESS_O}))
- PROGRESS_TIME $1
- case $PROGRESS_FT in
- S)
- PROGRESS_EXT=2
- PROGRESS_P_TMP=$PROGRESS_P
- PROGRESS_MR=$(($PROGRESS_MR+4))
- PROGRESS_R_TMP=$PROGRESS_R1
- PROGRESS_R1=─
- PROGRESS_I=┌
- PROGRESS_O=┐
- PROGRESS_P=
- PROGRESS_CALC $1
- PROGRESS_PREP T # draw top line of the frame
- printf "\n\n"
- PROGRESS_I=└
- PROGRESS_O=┘
- PROGRESS_PREP B # draw bottom line of the frame
- printf "\033[A"
- PROGRESS_I=│
- PROGRESS_O=│
- PROGRESS_R1=$PROGRESS_R_TMP
- PROGRESS_P=$PROGRESS_P_TMP
- PROGRESS_MR=$(($PROGRESS_MR-4))
- PROGRESS_FIN="\033[A"
- ;;
- D)
- PROGRESS_EXT=2
- PROGRESS_P_TMP=$PROGRESS_P
- PROGRESS_MR=$(($PROGRESS_MR+4))
- PROGRESS_R_TMP=$PROGRESS_R1
- PROGRESS_R1=═
- PROGRESS_I=╔
- PROGRESS_O=╗
- PROGRESS_P=
- PROGRESS_CALC $1
- PROGRESS_PREP T # draw top line of the frame
- printf "\n\n"
- PROGRESS_I=╚
- PROGRESS_O=╝
- PROGRESS_PREP B # draw bottom line of the frame
- printf "\033[A"
- PROGRESS_I=║
- PROGRESS_O=║
- PROGRESS_R1=$PROGRESS_R_TMP
- PROGRESS_P=$PROGRESS_P_TMP
- PROGRESS_MR=$(($PROGRESS_MR-4))
- PROGRESS_FIN="\033[A"
- ;;
- F)
- PROGRESS_EXT=2
- PROGRESS_P_TMP=$PROGRESS_P
- PROGRESS_MR=$(($PROGRESS_MR+4))
- PROGRESS_R_TMP=$PROGRESS_R1
- PROGRESS_TPOS=X
- PROGRESS_R1=▄
- PROGRESS_I=▄
- PROGRESS_O=▄
- PROGRESS_P=
- printf "\033["$PROGRESS_ML"C$PROGRESS_TOUT\n"
- PROGRESS_CALC $1
- PROGRESS_PREP T # draw top line of the frame
- printf "\n\n"
- PROGRESS_R1=▀
- PROGRESS_I=▀
- PROGRESS_O=▀
- PROGRESS_PREP B # draw bottom line of the frame
- printf "\033[A"
- if [ $1 -eq $PROGRESS_MAX1 ]; then
- PROGRESS_O=█
- else
- PROGRESS_O=▐
- fi
- PROGRESS_I=█
- PROGRESS_R1=$PROGRESS_R_TMP
- PROGRESS_P=$PROGRESS_P_TMP
- PROGRESS_MR=$(($PROGRESS_MR-4))
- PROGRESS_FIN="\033[2A"
- ;;
- T)
- [ -z ${2##*[!0-9]*} ] || [ $2 -lt 0 ] || [ $2 -gt $PROGRESS_MAX2 ] && return 1
- PROGRESS_EXT=2
- PROGRESS_P_TMP=$PROGRESS_P
- PROGRESS_MR=$(($PROGRESS_MR+4))
- PROGRESS_R_TMP=$PROGRESS_R1
- PROGRESS_R1=─
- PROGRESS_I=┌
- PROGRESS_O=┐
- PROGRESS_P=
- PROGRESS_CALC $1
- PROGRESS_PREP T # draw top line of the frame
- printf "\n\n"
- PROGRESS_I=├
- PROGRESS_O=┤
- PROGRESS_PREP M # draw middle line of the frame
- printf "\n\n"
- PROGRESS_I=└
- PROGRESS_O=┘
- PROGRESS_PREP B # draw bottom line of the frame
- printf "\033[3A"
- PROGRESS_I=│
- PROGRESS_O=│
- PROGRESS_R1=$PROGRESS_R_TMP
- PROGRESS_P=$PROGRESS_P_TMP
- PROGRESS_MR=$(($PROGRESS_MR-4))
- PROGRESS_FIN="\033[3A"
- ;;
- N)
- PROGRESS_EXT=2
- PROGRESS_R1="\033[7m━\033[0m"
- PROGRESS_L1=█
- PROGRESS_I=▐
- PROGRESS_O=▌
- ;;
- esac
- if [ $PROGRESS_FT != S ] && [ $PROGRESS_FT != D ] && [ $PROGRESS_FT != F ] && [ $PROGRESS_FT != T ]; then
- case $PROGRESS_TPOS in
- T)
- PROGRESS_TOUT="\033["$(($PROGRESS_ML))"C$PROGRESS_TOUT\n"
- PROGRESS_FIN="\033[A"
- ;;
- B)
- PROGRESS_TOUT="\n\033["$(($PROGRESS_ML))"C$PROGRESS_TOUT\033[A"
- ;;
- esac
- printf "$PROGRESS_TOUT"
- fi
- PROGRESS_CALC $1
- PROGRESS_DRAW $1
- if [ $PROGRESS_FT = T ] && [ -n "$2" ]; then
- PROGRESS_STAGE=2
- printf "\n\n"
- PROGRESS_CALC $2
- PROGRESS_DRAW $2
- PROGRESS_STAGE=1
- fi
- printf "$PROGRESS_FIN"
- }
- # ANSI escape sequences to move the cursor around the screen at will...
- # - Position the Cursor:
- # \033[<L>;<C>H Or \033[<L>;<C>f
- # puts the cursor at line L and column C.
- # - Move the cursor up N lines:
- # \033[<N>A
- # - Move the cursor down N lines:
- # \033[<N>B
- # - Move the cursor forward N columns:
- # \033[<N>C
- # - Move the cursor backward N columns:
- # \033[<N>D
- # - Clear the screen, move to (0,0):
- # \033[2J
- # - Erase to end of line:
- # \033[K
- # - Save cursor position:
- # \033[s
- # - Restore cursor position:
- # \033[u
- # ASCII characters to draw lines...
- # ═ ║ ╔ ╗ ╚ ╝ ╬ ╠ ╣ ╦ ╩
- # ─ │ ┌ ┐ └ ┘ ┼ ├ ┤ ┬ ┴
- # ╟ ╫ ╢ ╓ ╥ ╖ ╙ ╨ ╜
- # ╞ ╪ ╡ ╒ ╤ ╕ ╘ ╧ ╛
- # █ ▓ ▒ ░ ▄ ▀ ━ ▌ ▐ ■
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement