Created
December 10, 2019 16:58
-
-
Save Pandry/4332e71c8b7c6be4fb0c997ec8264f1f to your computer and use it in GitHub Desktop.
A simple script to show a spinner during long tasks in bash
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 | |
### | |
# 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