Skip to content

Instantly share code, notes, and snippets.

@filipekiss
Created March 13, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipekiss/20527d9a8e6817c2582eab3da219bf94 to your computer and use it in GitHub Desktop.
Save filipekiss/20527d9a8e6817c2582eab3da219bf94 to your computer and use it in GitHub Desktop.
Bash Activity Indicator
#!/usr/bin/env bash
#
# A simple activity indicator for bash scripting.
# Optional message can be passed to spinner function.
# This can be used on interactive shells and in scripts
#
# Scripting usage:
#
# e_spinner "Import MySQL"
# cat somefile.sql | mysql > /dev/null
# e_spinner_end
#
# Interactive Shell Usage:
# e_spinner_start "Slow running command"; sleep 10; e_spinner_end
#
e_spinner() {
local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
while sleep 0.1; do
e_reset_line
local temp=${spinstr#?}
printf " %s${RESET} ${2}%b${RESET}" "${spinstr:0:1}" "${1}"
local spinstr=$temp${spinstr%"$temp"}
done
}
e_spinner_start() {
local MESSAGE="$1"
e_spinner "$MESSAGE" &
E_SPINNER_PID=$!
disown $E_SPINNER_PID &> /dev/null #make sure bash doesn't freak out when we kill the spinner
}
e_spinner_end() {
kill $E_SPINNER_PID &> /dev/null
e_reset_line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment