diff options
| author | William Harrington <kb0iic@berzerkula.org> | 2021-03-26 23:10:09 -0500 | 
|---|---|---|
| committer | William Harrington <kb0iic@berzerkula.org> | 2021-03-26 23:47:18 -0500 | 
| commit | ebc7e4f32d27e0d3bb82d42db5d20f768948289f (patch) | |
| tree | d988d29c4b118791f3c14fc6ccda8655c635bd0b | |
| parent | 440eda1952025bb2d9a76bd8bf62c296ca80f956 (diff) | |
Replace spinner with random spinner selection when building. Possibility to pass an argument to spinner which chooses which spinner to use. For now it is random.
| -rw-r--r-- | functions.sh | 126 | 
1 files changed, 82 insertions, 44 deletions
| diff --git a/functions.sh b/functions.sh index 6b8671a..4b1f988 100644 --- a/functions.sh +++ b/functions.sh @@ -17,49 +17,87 @@ download()  export -f download -## spinner takes the pid of the process as the first argument and -#  string to display as second argument (default provided) and spins -#  until the process completes. -spinner() { -    local proc="$1" -    #local str="${2:-'Copyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆'}" -	local str="" -    local delay="0.1" -    tput civis  # hide cursor -    printf "%b" "${WHT}" -    while [ -d /proc/"$proc" ]; do -        #-\|/ -        #⠁⠂⠄⡀⢀⠠⠐⠈ -        #▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ -        #←↖↑↗→↘↓↙ -        #▖▘▝▗ -        #◢◣◤◥ -        #┤┘┴└├┌┬┐ -        #◰◳◲◱ -        #◴◷◶◵ -        #◐◓◑◒ -        #⣾⣽⣻⢿⡿⣟⣯⣷ -        printf '\033[s\033[u[ ▁ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▂ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▃ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▄ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▅ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▆ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▇ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ █ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▇ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▆ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▅ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▄ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▃ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▂ ] %s\033[u' "$str"; sleep "$delay" -        printf '\033[s\033[u[ ▁ ] %s\033[u' "$str"; sleep "$delay" -    done -    wait "${proc}" -    retval=$? -    printf '\033[s\033[u%*s\033[u\033[0m' $((${#str}+6)) " "  # return to normal -    tput cnorm  # restore cursor -    return $retval +# 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 shutdown() { +  tput cnorm # reset cursor +} +trap shutdown EXIT + +function cursorBack() { +  echo -en "\033[$1D"  } -export -f spinner +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 LC_CTYPE=C + +  local pid=$1 # Process Id of the previous running command + +  case $(($RANDOM % 12)) in +  0) +    local spin='⠁⠂⠄⡀⢀⠠⠐⠈' +    local charwidth=3 +    ;; +  1) +    local spin='-\|/' +    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 +    ;; +  esac + +  local i=0 +  tput civis # cursor invisible +  while kill -0 $pid 2>/dev/null; do +    local i=$(((i + $charwidth) % ${#spin})) +    printf "%s" "${spin:$i:$charwidth}" + +    cursorBack 1 +    sleep .1 +  done +  tput cnorm +  wait $pid # capture exit code +  return $? +} | 
