bashscripts/spinner.sh

71 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏")
FRAME_INTERVAL=0.1
RED="\e[31m"
GREEN="\e[32m"
ENDCOLOR="\e[0m"
start_spinner() {
#local step=0
local CMD=$1
local STEP=$2
tput civis -- invisible
#while [ "$step" -lt "${#CMDS[@]}" ]; do
#${CMDS[$step]} & pid=$!
$CMD & pid=$!
#status=$($CMD & pid=$!)
#status=$?
#pid=$!
while ps -p $pid &>/dev/null; do
#while kill -0 $pid 2>/dev/null; do
#echo -ne "\\r[ ] ${STEPS[$step]} ..."
echo -ne "\\r[ ] $STEP ..."
for k in "${!FRAME[@]}"; do
echo -ne "\\r[ ${FRAME[k]} ] "
sleep $FRAME_INTERVAL
done
done
wait "$pid"; status=$!
#echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
#echo -ne "\\r[ ✔ ] ${STEPS[$step]}\\n"
#re='^[0-9]+$'
#if ! [[ $status =~ $re ]]; then
# echo $status
#else
if [ $status -eq 0 ]; then
echo -ne "\\r[ ${GREEN}${ENDCOLOR} ] $STEP\\n"
else
echo -ne "\\r[ ${RED}${ENDCOLOR} ] $STEP\\n"
fi
#fi
#step=$((step + 1))
#done
tput cnorm -- normal
}
start_spinner "apt update -qq" "Search for updates"
start_spinner "apt upgrade -qq -y" "Apply updates"
start_spinner "./testsleep.sh" "Testing fail" #ha de donar FAIL!!
start_spinner "rm *.mp3" "Testing fail"
./testsleep.sh
echo $?
#/usr/bin/scp me@website.com:file somewhere 2>/dev/null &
#apt update -qq > /dev/null 2>/dev/null & pid=$! # Process Id of the previous running command
#spin='-\|/'
#i=0
#while kill -0 $pid 2>/dev/null
#do
# i=$(( (i+1) %4 ))
# printf "\r${spin:$i:1}"
# sleep .1
#done