Skip to content

Instantly share code, notes, and snippets.

@BlurryFlurry
Last active January 29, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlurryFlurry/3a1fa1ad90a32e062b4fca058627987a to your computer and use it in GitHub Desktop.
Save BlurryFlurry/3a1fa1ad90a32e062b4fca058627987a to your computer and use it in GitHub Desktop.
#!/bin/bash
declare process_echo_history
declare last_process_status
spinner() {
#spinner animation
local pid=$!
local delay=0.20
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
wait $pid
last_process_status=$?
printf " \b\b\b\b"
}
process_echo() {
local RED=$(tput setaf 1)
local GREEN=$(tput setaf 2)
local YELLOW=$(tput setaf 3)
local ENDCOLOR=$(tput sgr0)
local text="$1"
local text_color=${!2:-$(tput sgr0)}
local characters=${#text}
local start_col=$(($(tput cols) / 2 - $characters / 2))
local start_line=$(($(tput lines) / 2))
local spinner_col=$(($(tput cols) - 7))
tput civis
clear
echo -e "$process_echo_history"
tput cup $start_line $start_col
tput el
echo -en "${text_color}$text${ENDCOLOR}"
tput cup $start_line $spinner_col
spinner
p_status=$([ "$last_process_status" -eq 0 ] && echo "${GREEN}[DONE]${ENDCOLOR}" || echo "${RED}[FAIL]${ENDCOLOR}")
echo -e "${GREEN}${p_status}${ENDCOLOR}"
process_echo_history+="\n $text ${p_status}"
sleep 0.5
tput clear
echo -e "$process_echo_history $ENDCOLOR"
sleep 0.2
tput cvvis
tput cnorm
}
################################################## cut it from here ##############
# examples
failfunc(){
sleep 3
exit 1
}
sleep 2 &
process_echo "process 1 testing and testing 2 and testing3" RED
sleep 2 &
process_echo "process 2" GREEN
sleep 3 &
process_echo "process 3" YELLOW
failfunc &
process_echo "failfunc" YELLOW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment