Created
August 20, 2023 22:20
-
-
Save davlgd/6d0e06366b746f33ca6b9f2cdd38a3d4 to your computer and use it in GitHub Desktop.
Spinner for Shell commands
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 | |
spinner() { | |
local pid=$1 | |
local delay=0.1 | |
local spinstr="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" | |
local i=0 | |
local len=${#spinstr} | |
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do | |
i=$(( (i+1) % len )) | |
char=${spinstr:$i:1} | |
printf "\r%s Executing long command..." "$char" | |
sleep $delay | |
done | |
# Clear the entire line before printing "Done!" | |
printf "\r%b" " " | |
printf "\r" | |
} | |
# Usage example | |
long_command() { | |
sleep 10 | |
} | |
printf " Executing long command..." | |
long_command & | |
spinner $! | |
echo -e "\033[32m✓\033[0m Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment