This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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