diff options
Diffstat (limited to 'src/lib/libhbar.sh')
| -rw-r--r-- | src/lib/libhbar.sh | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/lib/libhbar.sh b/src/lib/libhbar.sh new file mode 100644 index 0000000..4489f09 --- /dev/null +++ b/src/lib/libhbar.sh @@ -0,0 +1,116 @@ +#!/bin/mksh -U +#include colors +#include xilib + +ADVANCE=" " + +move_up () { + [ ! "$1" = "0" ] && + printf "\033[%dA" "$1" "0" +} + +move_down () { + [ ! "$1" = "0" ] && + printf "\033[%dB" "$1" +} + +count_string () { + local c t + $human && { + c=$(format_bytes $completed) + t=$(format_bytes $total) + } || { + c=$1 + t=$2 + } + printf "[%s/%s]" $c $t +} + +hbar () { + local width terminate human text color reset unit line count + width=$(tput cols) + color="$BLACK$BG_WHITE" + reset="$WHITE$BG_DEFAULT" + terminate=false + human=false + # dont write any real characters ya + notext=false + line=0 + + while getopts "T:c:r:u:l:hnt" opt; do + case "$opt" in + T) + text="$OPTARG" + ;; + c) + color="$OPTARG" + ;; + r) + reset="$OPTARG" + ;; + u) + unit="$OPTARG" + ;; + t) + terminate=true + ;; + h) + human=true + ;; + l) + line="$OPTARG" + ;; + n) + notext=true + esac + done + + shift $((OPTIND-1)) + + [ "$#" -lt 2 ] && { + return 1 + } + + completed="$1" + total="$2" + + move_up $line + + count=$(count_string $completed $total) + #printf "\r$text" + printf "$RESET\r" + printf "$color" + reset_at=0 + + [ "$total" -gt "0" ] && reset_at=$(((completed*width)/total)) + + _x=0 + while [ "$_x" -lt "$width" ]; do + + if $notext; then + [ "$_x" -gt "$reset_at" ] && printf "$reset" || printf "$color" + printf "%s" "$ADVANCE" + else + [ "$_x" = "$reset_at" ] && printf "$reset" + if [ "${#text}" -gt 0 ]; then + printf "%s" "${text%%${text#?}}" + text="${text#?}" + else + if [ "$((width-${#count}))" = "$_x" ]; then + printf "%s" "${count%%${count#?}}" + + count="${count#?}" + else + printf "$ADVANCE" + fi + fi + fi + _x=$((_x+1)) + done + + move_down $line + + printf "$RESET" + $terminate && printf "\n" +} + |
