forked from MKessar/GameShell_git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_bar
More file actions
executable file
·280 lines (247 loc) · 6.45 KB
/
progress_bar
File metadata and controls
executable file
·280 lines (247 loc) · 6.45 KB
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
<
10000
div data-line-number="31" class="react-line-number react-code-text" style="padding-right:16px">3132
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env sh
# display a small animation until the given PID stops
. gsh_gettext.sh
TEXTDOMAIN=gsh
get_design() {
case "$1" in
0 | bat) design=bat ;;
1 | snake) design=snake ;;
2 | caterpillar) design=caterpillar ;;
3 | snail) design=snail ;;
4 | spider) design=spider ;;
5 | fish-back-and-forth) design=fish-back-and-forth ;;
6 | duck) design=duck ;;
7 | centipede) design=centipede ;;
8 | ant) design=ant ;;
9 | fish) design=fish ;;
10 | large-fish) design=large-fish ;;
11 | dots) design=dots ;;
12 | bar) design=bar ;;
13 | rotation) design=rotation ;;
esac
}
init_data() {
design=$1
case $design in
"dots")
# simple dots
STR='.'
PRE=''
POST='\n'
MSG=''
DELAY=0.2
;;
"bar")
# simple dots
STR='='
PRE='['
POST=']\n'
MSG=''
DELAY=0.2
;;
"rotation")
# rotating bar
STR='\b|
\b/
\b-
\b\'
PRE='-'
POST='\b \b'
MSG=""
DELAY=0.1
;;
"bat")
# flying bat
STR='\b\b\b \,/
\b\b\b \,/
\b\b\b \,/
\b\b\b \,/
\b\b\b /`\
\b\b\b /`\
\b\b\b /`\
\b\b\b /`\'
PRE=' '
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, a bat flies by...")\\n"
DELAY=0.1
;;
"snake")
# slithering snake
STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b __/\__/\__/\<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b _/\__/\__/\_<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b /\__/\__/\__<:>
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \__/\__/\__/<:>'
PRE='\__/\__/\__/<:>'
POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a snake slithers by...")\\n"
DELAY=0.2
;;
"caterpillar")
STR='\b\b\b\b\b\b\b\b\b ___^___@
\b\b\b\b\b\b\b\b __/\__@
\b\b\b\b\b\b\b _/^\_@
\b\b\b\b\b\b __/\__@
\b\b\b\b\b\b\b ___^___@
\b\b\b\b\b\b\b\b\b ________@'
PRE='_______@"'
POST='\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a caterpillar crawls by...")\\n"
DELAY=0.2
;;
"snail")
STR='\b\b\b\b\b\b\b_(@)__/"
\b\b\b\b\b\b\b\b__(@)_/"
\b\b\b\b\b\b\b\b _(@)_/"'
PRE='_(O)_/"'
POST='\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a snail goes by...")\\n"
DELAY=0.3
;;
"spider")
# FIXME: not sure I understand the escaping here!
STR='\b\b\b\b\b //o\\\\\\\\
\b\b\b\b\b //o\\\\\\\\
\b\b\b\b\b///o\\\\
\b\b\b\b\b///o\\\\'
PRE='//o\\\\'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a spider crawls by...")\\n"
DELAY=0.2
;;
"duck")
STR='\b\b\b\b\\_@<
\b\b\b\b \\_@=
\b\b\b\b\\_@=
\b\b\b\b\\_@<'
PRE='\\_@<'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a duck swims by...")\\n"
DELAY=0.1
;;
"fish-back-and-forth")
# Swimming fish (small) going back and forth
STR='\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b ><>
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><
\b\b\b \b\b\b\b<><'
PRE='><>'
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, you see a fish swimming in circles...")\\n"
DELAY=0.2
;;
"centipede")
# crawling centipede
STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ,`,`,`,`,`,`,`(:)
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`,`,`,`,`,`,`,(:)'
PRE='`,`,`,`,`,`,`,(:)'
POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a centipede crawls by...")\\n"
DELAY=0.2
;;
"ant")
# crawling ant
STR='\b\b\b\b\b >|<()'
PRE='>|<()'
POST='\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, an ant crawls by...")\\n"
DELAY=0.1
;;
"fish")
# Swimming fish (small)
STR='\b\b\b ><>'
PRE='><>'
POST='\b\b\b \r'
MSG="$(gettext "While you are waiting, a fish swims by...")\\n"
DELAY=0.2
;;
"large-fish")
# Swimming fish (large)
STR="\b\b\b\b\b\b\b\b ><(((('>"
PRE="><(((('>"
POST='\b\b\b\b\b\b\b\b \r'
MSG="$(gettext "While you are waiting, a large fish swims by...")\\n"
DELAY=0.2
;;
*)
echo "progress_bar: unknown progress kind: '$design'."
exit 1
;;
esac
}
animation() {
# Hide cursor.
tput civis 2>/dev/null
# NOTE: disabling echoing in terminal during animation doesn't always work in zsh!
# running
# ./utils/archive.sh -a missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires; ./gameshell.sh -Dqc "gsh auto"
# loops!
# Removing the stty -echo / stty echo makes the problem go away.
# I'm not sure what happens...
#
[ -n "$GSH_NON_INTERACTIVE" ] || stty -echo 2>/dev/null # see NOTE
# the additional \b\b\b \b\b\b is used to erase the possible ^C
# see NOTE
trap 'tput cnorm 2>/dev/null; [ -n "$GSH_NON_INTERACTIVE" ] || stty echo 2>/dev/null; printf "\b\b\b \b\b\b$POST";exit 1' INT TERM QUIT HUP
# Print initial message.
printf "$MSG"
# Make progress for each character read on [stdin].
printf "$PRE"
while kill -0 "$PID" 2>/dev/null
# we need to test that PID is still active here as well, because the "break"
# in the body of the loop takes place in a subshell. It is not possible to
# return from the function from there (using "break 2" or "return")!
do
printf "$STR\n" | while read -r line
do
printf "$line"
sleep $DELAY
kill -0 "$PID" 2>/dev/null || break
done
done
}
main() {
design=RANDOM
if [ "$1" = "-b" ]
then
design=$2
shift 2
fi
if [ -z "$*" ]
then
echo "usage: $0 [-d DESIGN] PID" >&2
cat <<EOS >&2
where DESIGN is one of bat, snake, caterpillar, snail, spider,
fish-back-and-forth, duck, centipede, ant, fish, large-fish, dots, bar,
rotation
EOS
exit 1
fi
PID=$1
get_design "$design"
if [ "$design" = RANDOM ]
then
n=$(RANDOM)
get_design "$((n%7))" # only modulo 7 to get the "interesting" animations
fi
init_data "$design"
if kill -0 "$PID" 2>/dev/null
then
animation
printf "$POST"
tput cnorm 2>/dev/null
[ -n "$GSH_NON_INTERACTIVE" ] || stty echo 2>/dev/null
fi
}
main "$@"