Skip to content

Instantly share code, notes, and snippets.

@nicolas-oliveira
Created April 14, 2025 15:20
Show Gist options
  • Save nicolas-oliveira/32285c9ae1fed875674af18dc5bd6e5a to your computer and use it in GitHub Desktop.
Save nicolas-oliveira/32285c9ae1fed875674af18dc5bd6e5a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Source: https://github.com/Silejonu/bash_loading_animations
# shellcheck disable=SC2034 # https://github.com/koalaman/shellcheck/wiki/SC2034
### Loading animations list ###
# The first value of an array is the interval (in seconds) between each frame
## ASCII animations ##
# Will work in any terminal, including the TTY.
CLASSIC=( 0.25 '-' "\\" '|' '/' )
BLA_box=( 0.2 ┤ ┴ ├ ┬ )
## UTF-8 animations ##
# Require Unicode support (will work in most modern terminals, but not in TTY).
# Some animations may not render properly with certain fonts.
CLASSIC_UTF8=( 0.25 '—' "\\" '|' '/' )
BOUNCE=( 0.3 '.' '·' '˙' '·' )
QUARTER=( 0.25 '▖' '▘' '▝' '▗' )
TRIANGLE=( 0.45 ◢ ◣ ◤ ◥)
BRAILLE=( 0.2 ⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈ )
BRAILLE_WHITESPACE=( 0.2 '⣾' '⣽' '⣻' '⢿' '⡿' '⣟' '⣯' '⣷' )
TRIGRAM=( 0.25 ☰ ☱ ☳ ☶ ☴ )
declare -a active_loading_animation
play_loading_animation_loop() {
while true ; do
for frame in "${active_loading_animation[@]}" ; do
printf "\r%s" "${frame}"
sleep "${loading_animation_frame_interval}"
done
done
}
start_loading_animation() {
active_loading_animation=( "${@}" )
# Extract the delay between each frame from array active_loading_animation
loading_animation_frame_interval="${active_loading_animation[0]}"
unset "active_loading_animation[0]"
tput civis # Hide the terminal cursor
play_loading_animation_loop &
loading_animation_pid="${!}"
}
stop_loading_animation() {
kill "${loading_animation_pid}" &> /dev/null
printf "\n"
tput cnorm # Restore the terminal cursor
}
###############################################################################
################################# USAGE GUIDE #################################
###############################################################################
################## Read below for the explanations on how to ##################
################### show loading animations in your script. ###################
###############################################################################
:<<'EXAMPLES'
## Put these lines at the top of your script:
## (replace /path/to/bash_loading_animations.sh with the appropriate filepath)
# Load in the functions and animations
source /path/to/bash_loading_animations.sh
# Run BLA::stop_loading_animation if the script is interrupted
trap BLA::stop_loading_animation SIGINT
# Show a loading animation for the command "foo"
BLA::start_loading_animation "${BLA_name_of_the_animation[@]}"
foo
BLA::stop_loading_animation
# If foo prints some output in the terminal, you may want to add:
foo 1> /dev/null # hide standard output
# or
foo 2> /dev/null # hide error messages
# or
foo &> /dev/null # hide all output
EXAMPLES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment