forked from Silejonu/bash_loading_animations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_loading_animations.sh
187 lines (168 loc) · 11.7 KB
/
bash_loading_animations.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
##### Table of contents #####
# 1. Loading animations list
# 2. Main code
# 3. Demo the animations
# 4. Usage guide
# 5. Compact versions of the main code
###################################### 1 ######################################
############# COPY THE ANIMATIONS BELOW TO THE TOP OF YOUR SCRIPT #############
###############################################################################
################### You can safely remove the lines for the ###################
#################### animations you are not interested in. ####################
###############################################################################
### 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 '-' '\' '|' '/' )
box=( 0.2 ┤ ┴ ├ ┬ )
bubble=( 0.6 · o O O o · )
breathe=( 0.9 ' () ' ' ( ) ' '( )' ' ( ) ' )
growing_dots=( 0.5 '. ' '.. ' '...' '.. ' '. ' ' ' )
passing_dots=( 0.25 '. ' '.. ' '...' ' ..' ' .' ' ' )
metro=( 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 . · ˙ · )
vertical_block=( 0.25 ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ █ ▇ ▆ ▅ ▄ ▃ ▁ )
horizontal_block=( 0.25 ▏ ▎ ▍ ▌ ▋ ▊ ▉ ▉ ▊ ▋ ▌ ▍ ▎ ▏ )
quarter=( 0.25 ▖ ▘ ▝ ▗ )
triangle=( 0.45 ◢ ◣ ◤ ◥)
semi_circle=( 0.1 ◐ ◓ ◑ ◒ )
rotating_eyes=( 0.4 ◡◡ ⊙⊙ ⊙⊙ ◠◠ )
firework=( 0.4 '⢀' '⠠' '⠐' '⠈' '*' '*' ' ' )
braille=( 0.2 ⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈ )
braille_whitespace=( 0.2 ⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷ )
trigram=( 0.25 ☰ ☱ ☳ ☶ ☴ )
arrow=( 0.15 ▹▹▹▹▹ ▸▹▹▹▹ ▹▸▹▹▹ ▹▹▸▹▹ ▹▹▹▸▹ ▹▹▹▹▸ ▹▹▹▹▹ ▹▹▹▹▹ ▹▹▹▹▹ ▹▹▹▹▹ ▹▹▹▹▹ ▹▹▹▹▹ ▹▹▹▹▹ )
bouncing_ball=( 0.4 '(● )' '( ● )' '( ● )' '( ● )' '( ● )' '( ●)' '( ● )' '( ● )' '( ● )' '( ● )' )
big_dot=( 0.7 ∙∙∙ ●∙∙ ∙●∙ ∙∙● )
modern_metro=( 0.15 ▰▱▱▱▱▱▱ ▰▰▱▱▱▱▱ ▰▰▰▱▱▱▱ ▱▰▰▰▱▱▱ ▱▱▰▰▰▱▱ ▱▱▱▰▰▰▱ ▱▱▱▱▰▰▰ ▱▱▱▱▱▰▰ ▱▱▱▱▱▱▰ ▱▱▱▱▱▱▱ ▱▱▱▱▱▱▱ ▱▱▱▱▱▱▱ ▱▱▱▱▱▱▱ )
pong=( 0.35 '▐⠂ ▌' '▐⠈ ▌' '▐ ⠂ ▌' '▐ ⠠ ▌' '▐ ⡀ ▌' '▐ ⠠ ▌' '▐ ⠂ ▌' '▐ ⠈ ▌' '▐ ⠂ ▌' '▐ ⠠ ▌' '▐ ⡀ ▌' '▐ ⠠ ▌' '▐ ⠂ ▌' '▐ ⠈ ▌' '▐ ⠂▌' '▐ ⠠▌' '▐ ⡀▌' '▐ ⠠ ▌' '▐ ⠂ ▌' '▐ ⠈ ▌' '▐ ⠂ ▌' '▐ ⠠ ▌' '▐ ⡀ ▌' '▐ ⠠ ▌' '▐ ⠂ ▌' '▐ ⠈ ▌' '▐ ⠂ ▌' '▐ ⠠ ▌' '▐ ⡀ ▌' '▐⠠ ▌' )
earth=( 0.45 🌍 🌎 🌏 )
clock=( 0.2 🕛 🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 )
moon=( 0.8 🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘 )
orange_pulse=( 0.35 🔸 🔶 🟠 🟠 🔶 )
blue_pulse=( 0.35 🔹 🔷 🔵 🔵 🔷 )
football=( 0.25 ' 👧⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️👦 ' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' '👧 ⚽️ 👦' )
blink=( 0.25 😐 😐 😐 😐 😐 😐 😐 😐 😐 😑 )
camera=( 0.1 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📷 📸 📷 📸 )
sparkling_camera=( 0.1 '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📷 ' '📸✨' '📷 ' '📸✨' )
sick=( 0.9 🤢 🤢 🤮 )
monkey=( 0.4 🙉 🙈 🙊 🙈 )
bomb=( 0.25 '💣 ' ' 💣 ' ' 💣 ' ' 💣' ' 💣' ' 💣' ' 💣' ' 💣' ' 💥' ' ' ' ' )
###################################### 2 ######################################
##### COPY THE CODE BELOW IN YOUR SCRIPT, RIGHT AFTER THE ANIMATIONS LIST #####
###############################################################################
############## If you would prefer to use a more compact version ##############
################# of this code, go to the bottom of the file. #################
###############################################################################
# Run stop_loading_animation if the script is interrupted
trap stop_loading_animation SIGINT
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 the active_loading_animation array
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
}
###################################### 3 ######################################
######################### DEMO THE LOADING ANIMATIONS #########################
###############################################################################
########## The code below serves as a way to preview the animations. ##########
################ You do not need to include it in your script. ################
###############################################################################
demo_interval() { sleep 7 ; kill "${loading_animation_pid}" &> /dev/null ; printf "\r \r" ; }
start_loading_animation "${classic[@]}" ; demo_interval
start_loading_animation "${box[@]}" ; demo_interval
start_loading_animation "${bubble[@]}" ; demo_interval
start_loading_animation "${breathe[@]}" ; demo_interval
start_loading_animation "${growing_dots[@]}" ; demo_interval
start_loading_animation "${passing_dots[@]}" ; demo_interval
start_loading_animation "${metro[@]}" ; demo_interval
start_loading_animation "${classic_utf8[@]}" ; demo_interval
start_loading_animation "${bounce[@]}" ; demo_interval
start_loading_animation "${vertical_block[@]}" ; demo_interval
start_loading_animation "${horizontal_block[@]}" ; demo_interval
start_loading_animation "${quarter[@]}" ; demo_interval
start_loading_animation "${triangle[@]}" ; demo_interval
start_loading_animation "${semi_circle[@]}" ; demo_interval
start_loading_animation "${rotating_eyes[@]}" ; demo_interval
start_loading_animation "${firework[@]}" ; demo_interval
start_loading_animation "${braille[@]}" ; demo_interval
start_loading_animation "${braille_whitespace[@]}" ; demo_interval
start_loading_animation "${trigram[@]}" ; demo_interval
start_loading_animation "${arrow[@]}" ; demo_interval
start_loading_animation "${bouncing_ball[@]}" ; demo_interval
start_loading_animation "${big_dot[@]}" ; demo_interval
start_loading_animation "${modern_metro[@]}" ; demo_interval
start_loading_animation "${pong[@]}" ; demo_interval
start_loading_animation "${earth[@]}" ; demo_interval
start_loading_animation "${clock[@]}" ; demo_interval
start_loading_animation "${moon[@]}" ; demo_interval
start_loading_animation "${orange_pulse[@]}" ; demo_interval
start_loading_animation "${blue_pulse[@]}" ; demo_interval
start_loading_animation "${football[@]}" ; demo_interval
start_loading_animation "${blink[@]}" ; demo_interval
start_loading_animation "${camera[@]}" ; demo_interval
start_loading_animation "${sparkling_camera[@]}" ; demo_interval
start_loading_animation "${sick[@]}" ; demo_interval
start_loading_animation "${monkey[@]}" ; demo_interval
start_loading_animation "${bomb[@]}" ; demo_interval
exit 0
###################################### 4 ######################################
################################# USAGE GUIDE #################################
###############################################################################
################## Read below for the explanations on how to ##################
################### show loading animations in your script. ###################
###############################################################################
# Show a loading animation for the command "foo"
start_loading_animation "${name_of_the_animation[@]}"
foo
stop_loading_animation
# If the command 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
###################################### 5 ######################################
###################### USE A COMPACT VERSION OF THE CODE ######################
###############################################################################
######### Copy one of the paragraphs below at the top of your script, #########
####################### just after the animations list. #######################
###############################################################################
# Max width = 80
trap stop_loading_animation SIGINT ; 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=( "${@}" ) ; \
loading_animation_frame_interval="${active_loading_animation[0]}" ; unset \
"active_loading_animation[0]" ; tput civis ; play_loading_animation_loop & \
loading_animation_pid="${!}" ; } ; stop_loading_animation() { kill \
"${loading_animation_pid}" &> /dev/null ; printf "\n" ; tput cnorm ; }
# Each function on a single line
trap stop_loading_animation SIGINT
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=( "${@}" ) ; loading_animation_frame_interval="${active_loading_animation[0]}" ; unset "active_loading_animation[0]" ; tput civis ; play_loading_animation_loop & loading_animation_pid="${!}" ; }
stop_loading_animation() { kill "${loading_animation_pid}" &> /dev/null ; printf "\n" ; tput cnorm ; }
# Single line
trap stop_loading_animation SIGINT ; 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=( "${@}" ) ; loading_animation_frame_interval="${active_loading_animation[0]}" ; unset "active_loading_animation[0]" ; tput civis ; play_loading_animation_loop & loading_animation_pid="${!}" ; } ; stop_loading_animation() { kill "${loading_animation_pid}" &> /dev/null ; printf "\n" ; tput cnorm ; }