-
Notifications
You must be signed in to change notification settings - Fork 9
/
.demoscript
executable file
·448 lines (395 loc) · 10.9 KB
/
.demoscript
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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# See: https://github.com/duglin/tools/tree/master/demoscript
#
# SAVE=1 Save output to a tar file for off-line running
# SKIP=1 Do not wait for user to press a key to continue
# RECOVER=1 Use the canned output when a command fails
# USESAVED=1 Use canned output instead of running the commands
# USESAVED=2 Use canned output IFF it exists, otherwise run it
scriptName="${0##.*/}"
bold=$(tput bold)
normal=$(tput sgr0)
delay=${DELAY:-"0.02"}
skip="$SKIP"
save="$SAVE"
saveTar=$(cd $(dirname "$0");pwd)/${scriptName}.tar
recover="$RECOVER"
useSaved="$USESAVED"
trap clean EXIT
function clean {
rm -f out cmds
}
if [[ "${useSaved}" != "" && "${useSaved}" != "2" && ! -e "${saveTar}" ]]; then
echo "Missing saved output file: ${saveTar}"
exit 1
fi
if [[ "${save}" != "" && "${useSaved}" == "" ]]; then
rm -f "${saveTar}"
fi
function myscript() {
if [[ "$(uname)" == Darwin ]]; then
script -q -a /dev/null $*
else
script -efq -a /dev/null -c "$*"
fi
}
function slowOutput() {
oldIFS=$IFS
IFS=""
while read -s -n 1 ch ; do
echo -n "$ch"
sleep 0.00001
done
IFS=$oldIFS
}
function slowType() {
str="$*"
if [[ "$delay" == "0" ]]; then
echo -n $bold$str$normal
return
fi
for i in `seq 0 ${#str}`; do
echo -n $bold${str:$i:1}$normal
sleep $delay
done
}
function slowTty() {
str="$*"
echo -n $bold >&3
if [[ "$delay" == "0" ]]; then
echo -n "$str"
else
for i in `seq 0 ${#str}`; do
echo -n "${str:$i:1}"
sleep $delay
done
fi
sleep 0.2 # just to give the other program time to show its input
echo -n $normal >&3
}
function readChar() {
read -s -n 1 ch
# deal with escape sequences
# ESC[5~ and ESC[6~ are for a mac's page up/down
case "$ch" in
) read -s -n 1 ch
if [[ "$ch" == "[" ]]; then
read -s -n 1 ch
if [[ "$ch" == "5" || "$ch" == "6" ]]; then
read -s -n 1 ch # should be ~
fi
fi
;;
f ) delay="0" ;;
s ) delay=${DELAY:-"0.02"} ;;
r ) skip="x" ;;
esac
}
function pause() {
if [[ "$skip" == "" ]]; then
readChar
else
sleep 0.2
fi
}
cmdNum=0
function doit() {
local ignorerc=""
local shouldfail=""
local noexec=""
local fakeit=${useSaved:-}
local noscroll=${NOSCROLL:-}
local postcmd=""
local retryonfail=${RETRYONFAIL:-}
local untilgrep=""
local pauseTime="1"
local norepaint=""
local slowoutput=""
local showcmd=""
while [[ "$1" == "--"* ]]; do
opt="$1"
shift
case "$opt" in
--ignorerc ) ignorerc="1" ;;
--shouldfail ) shouldfail="1" ;;
--noexec ) noexec="1" ;;
--usesaved ) fakeit="1" ;;
--noscroll ) noscroll="1" ;;
--scroll ) noscroll="" ;;
--retryonfail ) retryonfail="1" ;;
--post* ) postcmd="${opt#*=}" ;;
--untilgrep* ) untilgrep="${opt#*=}" ;;
--pausetime* ) pauseTime="${opt#*=}" ;;
--norepaint ) norepaint="1" ;;
--slowoutput ) slowoutput="1" ;;
--showcmd* ) showcmd="${opt#*=}" ;;
esac
done
let cmdNum=cmdNum+1
while true ; do
set +e
echo -n $bold"$"$normal" "
pause
slowType ${showcmd:-$*}
echo "$*" >> cmds
pause
echo
local lines=$(tput lines)
let lines=lines-3
moreCMD="more -$lines"
if [[ "$skip" != "" || "$noscroll" != "" ]]; then
moreCMD="cat"
fi
if [[ "$postcmd" != "" ]]; then
moreCMD="$postcmd | $moreCMD"
fi
daCmd=" $* "
# Unless we're told to not execute it, do it
if [[ "$noexec" == "" ]]; then
# Run the cmd
oldSize=0
version=0
while true ; do
saveFile="run.${cmdNum}"
if [[ -n "${untilgrep}" ]]; then
let version=version+1
saveFile="${saveFile}.${version}"
fi
if [[ "$fakeit" != "" ]]; then
# Faking it!
daCmd="tar -xOf ${saveTar} ${saveFile}"
fi
if [[ "$fakeit" != "" ]] && [[ -n "${slowoutput}" ]]; then
daCmd="${daCmd} | bash -c 'IFS= ; while read -n1 -d ch ; do echo -n \$ch ; x=0 ; while ((x<300)); do let x=x+1 ; done ; done'"
fi
if [[ -n "${untilgrep}" ]]; then
if [[ -z "${norepaint}" ]] && (( oldSize != 0 )); then
printf "\033[%dA" $oldSize
fi
bash -c "${daCmd}" 2>&1 | tee out | while read a ; do
echo -n "$a"
printf "\033[K\\n"
done
rc=${PIPESTATUS[0]}
newSize=$(wc -l out | sed "s/ .*//")
if [[ -z "${norepaint}" ]]; then
while (( newSize < oldSize )); do
printf "\033[K\\n"
let newSize=newSize+1
done
fi
oldSize=${newSize}
# bash -c "${daCmd}" > out 2>&1
# rc=$?
# if [[ -z "${norepaint}" ]] && (( oldSize != 0 )); then
# let size=0
# printf "\033[%dA" $oldSize
# while (( size < oldSize )); do
# printf "\033[K\\n"
# let size=size+1
# done
# printf "\033[%dA" $oldSize
# fi
# cat out | while read a ; do
# echo -n "$a"
# printf "\033[K\\n"
# done
else
bash -c "${daCmd}" 2>&1 | tee out | eval ${moreCMD[@]}
rc=${PIPESTATUS[0]}
fi
# Save the output if we're asked to
if [[ "$fakeIt" == "" && "$save" != "" ]]; then
cp out "${saveFile}"
# tar --delete -f "${saveTar}" "${saveFile}" > /dev/null 2>&1 || true
tar -rf "${saveTar}" "${saveFile}"
rm "${saveFile}"
fi
if [[ -z "${untilgrep}" ]]; then
break
fi
if grep "${untilgrep}" out > /dev/null 2>&1 ; then
break
else
read -s -n 1 -t "${pauseTime}" ch || true
if [[ "$ch" != "" ]]; then
break
fi
fi
done
# If the cmd failed see if we should use the canned output
if [[ "$recover" != "" ]]; then
if [[ ( ( "$shouldfail" == "" && "$rc" != "0" ) || \
( "$shouldfail" != "" && "$rc" == "0" ) ) ]] && \
tar -xf "${saveTar}" "${saveFile}" > /dev/null 2>&1 ; then
# echo "** Using saved output ${saveFile} **"
cp "${saveFile}" out
rm "${saveFile}"
fi
fi
else
# We're not really executing it, just showing the cmd
echo -n > out
rc=0
fi
echo
errorMsg=""
if [[ "$ignorerc" == "" ]]; then
# We're not totally ignoring the exit code
if [[ "$shouldfail" != "" ]]; then
# We need to make sure the command failed as expected
if [[ "$rc" == "0" ]]; then
errorMsg="Expected non-zero exit code, got: $rc"
fi
else
# Normal non-zero exit code expected case
if [[ "$rc" != "0" ]]; then
errorMsg="Non-zero exit code: $rc"
fi
fi
fi
set -e
if [[ "${errorMsg}" != "" ]]; then
echo -n "${errorMsg}"
if [[ "${retryonfail}" != "" ]]; then
# Force them to hit a key since they need to fix stuff
echo -n ". Press any key to try again"
readChar
echo
continue
fi
echo
exit 1
fi
break
done
}
function background() {
echo -n $bold"$"$normal" "
pause
slowType $*
echo "$*" >> cmds
pause
echo
if [[ "$useSaved" == "" ]]; then
bash -c " $* " &
fi
}
function ttyDoit() {
local ignorerc=""
local shouldfail=""
while [[ "$1" == "--"* ]]; do
opt="$1"
shift
case "$opt" in
--ignorerc ) ignorerc="1" ;;
--shouldfail ) shouldfail="1" ;;
esac
done
echo -n $bold"$"$normal" "
pause
slowType "$*"
pause
echo
exec 3>&1
set +e
(
sleep 0.2
while read -u 10 line ; do
dontWait=""
if [[ "$line" == "run "* ]]; then
line=${line:4}
${line}
continue
fi
if [[ "$line" == "@"* ]]; then
# Lines starting with "@" will be executed
# immediately w/o pausing before or after showing it
dontWait="x"
line=${line:1}
fi
if [[ "$dontWait" == "" ]]; then pause ; fi
slowTty $line
if [[ "$dontWait" == "" ]]; then pause ; fi
echo
sleep 0.2
done
echo
) | myscript $*
rc=${PIPESTATUS[1]}
echo -n $normal
echo
[[ "$ignorerc" == "" && "$rc" != "0" ]] && echo "Non-zero exit code" && exit 1
[[ "$shouldfail" != "" && "$rc" == "0" ]] && echo "Expected non-zero exit code" && exit 1
set -e
}
function comment() {
local LF="\\n"
local CR=${LF}
local echoopt=""
local dopause=""
local dopauseafter=""
local nohash=""
while [[ "$1" == "--"* ]]; do
opt="$1"
shift
case "$opt" in
--nolf ) LF="" ;;
--nocr ) CR="" ; LF="" ;;
--pause ) dopause="1" ; dopauseafter="1" ;;
--pauseafter ) dopauseafter="1" ;;
--nohash ) nohash="1" ;;
esac
done
if [[ "$nohash" == "" ]]; then
echo -en $bold\#" "$normal
fi
if [[ "$dopause" == "1" ]]; then
pause
fi
echo -en ${echoopt} "$bold$*$normal"
if [[ "$dopause" == "1" || "$dopauseafter" == "1" ]]; then
pause
fi
echo -en ${echoopt} "${CR}${LF}"
}
# Wait until the passed in cmd returns true
function wait() {
# set -x
if [[ "${useSaved}" != "" ]]; then
return
fi
if [ "$1" == "!" ]; then
shift
while (bash -c " $* " &> /dev/null); do
sleep 1
done
else
while !(bash -c " $* " &> /dev/null); do
sleep 1
done
fi
# set +x
}
function scroll() {
local lines=$(tput lines)
let lines=lines-3
echo -n $bold"$"$normal" "
# set +e
pause
if [[ "$skip" == "" ]]; then
slowType more $*
else
slowType cat $*
fi
pause
echo
if [[ "$skip" == "" ]]; then
more -$lines $*
else
cat $*
fi
echo
}