From 6e6788715e23435f33a8572cc7cc48fabf180a40 Mon Sep 17 00:00:00 2001 From: lework Date: Wed, 18 Mar 2020 13:52:30 +0800 Subject: [PATCH] add wait process --- shell/library.sh | 12 ++++++ shell/wait-process.sh | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 shell/wait-process.sh diff --git a/shell/library.sh b/shell/library.sh index 52138f8..38449cc 100644 --- a/shell/library.sh +++ b/shell/library.sh @@ -783,3 +783,15 @@ func_secret_input(){ echo "" } + +# Check if a program is installed +command_exists() { + # check if command exists and fail otherwise + command -v "$1" >/dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "I require $1 but it's not installed. Abort." + exit 1 + fi +} + + diff --git a/shell/wait-process.sh b/shell/wait-process.sh new file mode 100644 index 0000000..a94ee64 --- /dev/null +++ b/shell/wait-process.sh @@ -0,0 +1,90 @@ +#!/bin/bash +# 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" +} + +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 + echo "123" + return $? +} + +("$@") & + +spinner $!