Skip to content

Instantly share code, notes, and snippets.

@Pandry
Created December 10, 2019 16:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pandry/4332e71c8b7c6be4fb0c997ec8264f1f to your computer and use it in GitHub Desktop.
Save Pandry/4332e71c8b7c6be4fb0c997ec8264f1f to your computer and use it in GitHub Desktop.
A simple script to show a spinner during long tasks in bash
#!/bin/bash
###
# Author: github.com/Pandry
# Version: v1.0
# Description: A simple function to create a spinner while executing a long command
# You can easily add your spinner by putting every "frame" into the spinarr array :)
###
spinner()
{
local -a spinarr=('⊶ ' '⊷ ')
local delay=0.25
#Some other spinners from the awesome sindresorhus' cli-spinners (https://github.com/sindresorhus/cli-spinners)
#local -a spinarr=('⣾' '⣽' '⣻' '⢿' '⡿' '⣟' '⣯' '⣷')
#local -a spinarr=('⢀⠀' '⡀⠀' '⠄⠀' '⢂⠀' '⡂⠀' '⠅⠀' '⢃⠀' '⡃⠀' '⠍⠀' '⢋⠀' '⡋⠀' '⠍⠁' '⢋⠁' '⡋⠁' '⠍⠉' '⠋⠉' '⠋⠉' '⠉⠙' '⠉⠙' '⠉⠩' '⠈⢙' '⠈⡙' '⢈⠩' '⡀⢙' '⠄⡙' '⢂⠩' '⡂⢘' '⠅⡘' '⢃⠨' '⡃⢐' '⠍⡐' '⢋⠠' '⡋⢀' '⠍⡁' '⢋⠁' '⡋⠁' '⠍⠉' '⠋⠉' '⠋⠉' '⠉⠙' '⠉⠙' '⠉⠩' '⠈⢙' '⠈⡙' '⠈⠩' '⠀⢙' '⠀⡙' '⠀⠩' '⠀⢘' '⠀⡘' '⠀⠨' '⠀⢐' '⠀⡐' '⠀⠠' '⠀⢀' '⠀⡀')
#local -a spinarr=('⠁' '⠉' '⠙' '⠚' '⠒' '⠂' '⠂' '⠒' '⠲' '⠴' '⠤' '⠄' '⠄' '⠤' '⠴' '⠲' '⠒' '⠂' '⠂' '⠒' '⠚' '⠙' '⠉' '⠁')
#local -a spinarr=('◢' '◣' '◤' '◥')
#local -a spinarr=('▹▹▹▹▹' '▸▹▹▹▹' '▹▸▹▹▹' '▹▹▸▹▹' '▹▹▹▸▹' '▹▹▹▹▸')
#local delay=0.1
local i=0
while [ "$(ps a | awk '{print $1}' | grep $1)" ]; do
local str=" ${spinarr[$(( $i % ${#spinarr[@]} ))]} $2"
printf "%s" "$str"
i=$(($i+1))
sleep $delay
for (( j=0; $j < ${#str}; j++ )); do printf "\b"; done;
done
for (( j=0; $j < ${#str}; j++ )); do printf "\b"; done;echo;
}
# Usage example (just copy&paste into a file, then chmod +x and execute):
sleep 10 && echo 'Hello world!' &
spinner $! "Calculating the string..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment