Skip to content

Commit

Permalink
fix: smooth execute-and-wait.zsh spinner animation
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster committed Jun 1, 2023
1 parent 5bd3c2b commit 8a8f2f4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions bin/.local/bin/execute-and-wait.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ local __resultvar=$3

eval $1 > /tmp/execute-and-wait.log 2>&1 &
pid=$!
delay=0.05
frames=('' '' '' '' '' '' '' '' '' '')
delay=0.08
frames=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)

echo "$pid" > "/tmp/.spinner.pid"

Expand All @@ -27,15 +27,20 @@ tput civis
index=0
framesCount=${#frames[@]}
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
builtin print -f "${YELLOW}${frames[$index]}${NC} ${GREEN}$2${NC}"

let index=index+1
if [ "$index" -ge "$framesCount" ]; then
index=0
lim=$(tput cols)
if [[ $index -eq 0 ]]; then
index+=1
fi
printf ' %.0s' {1..$lim}
echo -n "\r"
builtin print -f "${YELLOW}${frames[$index]}${NC} ${GREEN}$2${NC}"

printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
index=$(( $(( $index + 1 )) % $(( ${#frames} + 1 )) ))
# Calculate the screen width
# Clear the line and move the cursor to the start
sleep $delay
# Return to the beginning of the line
echo -n "\r"
done

#
Expand All @@ -53,6 +58,6 @@ fi
# Restore the cursor
tput cnorm

eval "$__resultvar=$exitCode"
return $exitCode

# vim: set expandtab filetype=zsh shiftwidth=4 softtabstop=4 tabstop=4:

0 comments on commit 8a8f2f4

Please sign in to comment.