diff options
Diffstat (limited to 'functions.sh')
-rw-r--r-- | functions.sh | 246 |
1 files changed, 123 insertions, 123 deletions
diff --git a/functions.sh b/functions.sh index b343e65..88a08d7 100644 --- a/functions.sh +++ b/functions.sh @@ -1,123 +1,123 @@ -#!/bin/bash - -download() -{ - tput civis - local url=$1 - printf "%b" "${IWHT} " - wget -nc -q --show-progress --progress=dot "$url" 2>&1 | grep --line-buffered "%" | \ - sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}' - local retval="${PIPESTATUS[0]}" - if [[ "${retval}" -ne 0 ]]; then - printf "%b" "${RED}FAILED! WGET EXIT (${retval})\n${RST}" - exit "${retval}" - fi - tput cnorm -} - -export -f download - -# Shows a spinner while another command is running. Randomly picks one of 12 spinner styles. -# @args command to run (with any parameters) while showing a spinner. -# E.g. ‹spinner sleep 10› - -function cursorBack() { - echo -en "\033[$1D" -} - -function spinner() { - # make sure we use non-unicode character type locale - # (that way it works for any locale as long as the font supports the characters) - local numspinners=12 - - local LC_CTYPE=C - - local pid=$1 # Process Id of the previous running command - - local spintype=$2 - - if [[ -z "${spintype}" ]] || [[ "${spintype}" -ge "${numspinners}" ]]; then - spintype=$((RANDOM % numspinners)) - fi - - case "${spintype}" in - 0) - local spin='-\|/' - local charwidth=1 - ;; - 1) - local spin='.oO0Oo' - local charwidth=1 - ;; - 2) - local spin='⠁⠂⠄⡀⢀⠠⠐⠈' - local charwidth=3 - ;; - 3) - local spin="▁▂▃▄▅▆▇█▇▆▅▄▃▂▁" - local charwidth=3 - ;; - 4) - local spin="▉▊▋▌▍▎▏▎▍▌▋▊▉" - local charwidth=3 - ;; - 5) - local spin='←↖↑↗→↘↓↙' - local charwidth=3 - ;; - 6) - local spin='▖▘▝▗' - local charwidth=3 - ;; - 7) - local spin='┤┘┴└├┌┬┐' - local charwidth=3 - ;; - 8) - local spin='◢◣◤◥' - local charwidth=3 - ;; - 9) - local spin='◰◳◲◱' - local charwidth=3 - ;; - 10) - local spin='◴◷◶◵' - local charwidth=3 - ;; - 11) - local spin='◐◓◑◒' - local charwidth=3 - ;; - 12) - local spin='⣾⣽⣻⢿⡿⣟⣯⣷' - local charwidth=3 - ;; - esac - - local i=0 - tput civis # cursor invisible - tput bold setaf 7 - while kill -0 "$pid" 2>/dev/null; do - local i=$(((i + charwidth) % ${#spin})) - printf "%b" "${spin:$i:$charwidth}" - - cursorBack 1 - sleep .2 - done - tput cnorm - wait "$pid" # capture exit code - return $? -} - -fail() -{ - printf "%b" "${IRED}${1}${RST}\n" -} -export -f fail - -success() -{ - printf "%b" "${IGRN}${1}${RST}\n" -} -export -f success +#!/bin/bash
+
+download()
+{
+ tput civis
+ local url=$1
+ printf "%b" "${IWHT} "
+ wget -nc -q --show-progress --progress=dot "$url" 2>&1 | grep --line-buffered "%" | \
+ sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
+ local retval="${PIPESTATUS[0]}"
+ if [[ "${retval}" -ne 0 ]]; then
+ printf "%b" "${RED}FAILED! WGET EXIT (${retval})\n${RST}"
+ exit "${retval}"
+ fi
+ tput cnorm
+}
+
+export -f download
+
+# Shows a spinner while another command is running. Randomly picks one of 12 spinner styles.
+# @args command to run (with any parameters) while showing a spinner.
+# E.g. ‹spinner sleep 10›
+
+function cursorBack() {
+ echo -en "\033[$1D"
+}
+
+function spinner() {
+ # make sure we use non-unicode character type locale
+ # (that way it works for any locale as long as the font supports the characters)
+ local numspinners=12
+
+ local LC_CTYPE=C
+
+ local pid=$1 # Process Id of the previous running command
+
+ local spintype=$2
+
+ if [[ -z "${spintype}" ]] || [[ "${spintype}" -ge "${numspinners}" ]]; then
+ spintype=$((RANDOM % numspinners))
+ fi
+
+ case "${spintype}" in
+ 0)
+ local spin='-\|/'
+ local charwidth=1
+ ;;
+ 1)
+ local spin='.oO0Oo'
+ local charwidth=1
+ ;;
+ 2)
+ local spin='⠁⠂⠄⡀⢀⠠⠐⠈'
+ local charwidth=3
+ ;;
+ 3)
+ local spin="▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
+ local charwidth=3
+ ;;
+ 4)
+ local spin="▉▊▋▌▍▎▏▎▍▌▋▊▉"
+ local charwidth=3
+ ;;
+ 5)
+ local spin='←↖↑↗→↘↓↙'
+ local charwidth=3
+ ;;
+ 6)
+ local spin='▖▘▝▗'
+ local charwidth=3
+ ;;
+ 7)
+ local spin='┤┘┴└├┌┬┐'
+ local charwidth=3
+ ;;
+ 8)
+ local spin='◢◣◤◥'
+ local charwidth=3
+ ;;
+ 9)
+ local spin='◰◳◲◱'
+ local charwidth=3
+ ;;
+ 10)
+ local spin='◴◷◶◵'
+ local charwidth=3
+ ;;
+ 11)
+ local spin='◐◓◑◒'
+ local charwidth=3
+ ;;
+ 12)
+ local spin='⣾⣽⣻⢿⡿⣟⣯⣷'
+ local charwidth=3
+ ;;
+ esac
+
+ local i=0
+ tput civis # cursor invisible
+ tput bold setaf 7
+ while kill -0 "$pid" 2>/dev/null; do
+ local i=$(((i + charwidth) % ${#spin}))
+ printf "%b" "${spin:$i:$charwidth}"
+
+ cursorBack 1
+ sleep .2
+ done
+ tput cnorm
+ wait "$pid" # capture exit code
+ return $?
+}
+
+fail()
+{
+ printf "%b" "${IRED}${1}${RST}\n"
+}
+export -f fail
+
+success()
+{
+ printf "%b" "${IGRN}${1}${RST}\n"
+}
+export -f success
|