-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.sh
executable file
·596 lines (522 loc) · 11.9 KB
/
table.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env bash
#####################################
# SETUP / TEARDOWN
#####################################
set -euo pipefail
stty_orig=$(stty -g) # capture the original keypress settings
FLIP=FLIP
SCAN=SCAN
CLEAR=CLEAR
HELP=HELP
function setup() {
if [[ $SCENE != $HELP ]]; then
tput civis # invisible cursor
stty -echo # hide user input
if [[ $SCENE != $CLEAR ]]; then
tput smcup # save and hide screen
fi
fi
}
trap cleanup 1 2 3 6 EXIT
function cleanup() {
if [[ $SCENE != $HELP ]]; then
tput cnorm # show cursor
stty ${stty_orig} # enable normal keypress echo
if [[ $SCENE != $CLEAR ]]; then
tput rmcup # replace screen
fi
fi
exit
}
#####################################
# STRING UTILS
#####################################
function makeString() {
local char=$1
local targetLength=$2
local acc=""
for ((i=0; i < ${targetLength}; i++)); do
acc="$acc$char"
done
echo "$acc"
}
function contains() {
local LIST=$1
local VALUE=$2
local DELIMITER=${3:-" "}
[[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]]
}
function repeat() {
local char=$1
local times=$2
local acc=""
for ((i = 0; i < $times; i++)); do
acc="$acc$char"
done
echo "$acc"
}
function empty() {
local length=$1
repeat " " "$length"
}
function getColumn () {
local COL
local ROW
IFS=';' read -sdR -p $'\E[6n' ROW COL
echo "${COL}"
}
function putWord() {
tput cup 0 0
echo -n "$@"
}
function getWidth() {
local column=$(getColumn)
local width=$((column - 1))
echo $width
}
function delWord() {
tput cup 0 0
empty $1
}
#####################################
# MATH UTILS
#####################################
function divide() {
# rather than (($1 / $2)) which only supports integer results
# awk division will return decimal results
awk "BEGIN {print $1 / $2}"
}
function getCenterStart() {
local item="$1"
putWord $item
local itemLength=$(getWidth)
delWord $itemLength
local center=$((cols / 2))
if [ $((center % 2)) -eq 1 ]; then
((center++))
fi
local halfLength=$((itemLength / 2))
echo $((center - halfLength))
}
#####################################
# DEFAULT PARTS
#####################################
EYE="°"
MOUTH="□"
CHEEK_LEFT="("
CHEEK_RIGHT=")"
FLIP_ARM="╯"
MOTION="︵"
#####################################
# ACTORS
#####################################
BEAR=bear
BURNS=burns
DISS=diss
JAKE=jake
PWNY=pwny
RAGE=rage
SCREAM=scream
ZEN=zen
actors="$BEAR $BURNS $DISS $JAKE $PWNY $RAGE $SCREAM $ZEN"
function configureActor() {
case $1 in
$BEAR)
EYE="•"
MOUTH="ᴥ"
FLIP_ARM="ノ"
CHEEK_LEFT="ʕ"
CHEEK_RIGHT="ʔ"
;;
$BURNS)
EYE="◉"
MOUTH="Д"
FLIP_ARM="┛"
MOTION="彡"
;;
$DISS)
EYE="ಥ"
MOUTH="_"
CHEEK_LEFT="«"
CHEEK_RIGHT="»"
;;
$JAKE)
EYE="❍"
MOUTH="ᴥ"
FLIP_ARM="┛"
MOTION="彡"
;;
$SCREAM)
FLIP_ARM="╭o͡͡͡"
CHEEK_RIGHT=" ༽"
CHEEK_LEFT="༼"
EYE=" ʘ̆ "
MOUTH=""
;;
$PWNY)
EYE="⊙"
MOUTH="▂"
FLIP_ARM="✖"
;;
$RAGE)
EYE="ಠ"
MOUTH="益"
FLIP_ARM="ノ"
MOTION="彡"
;;
$ZEN)
EYE="︶"
MOUTH="_"
FLIP_ARM="╭∩╮"
;;
esac
}
#####################################
# CLI ARGS
#####################################
SCENE=$FLIP
while [[ $# -gt 0 ]]; do
case $1 in
# options
-f|--fps)
fps="$2"
shift # past argument
shift # past value
;;
-h|--help)
SCENE=$HELP
shift # past argument
;;
# actors
-a|--actor)
ACTOR=$2
if ! contains "$actors" "$ACTOR"; then
echo Unknown actor: $ACTOR
echo Choose one of: $(echo $actors | sed 's/ /, /g')
exit 1
fi
configureActor $ACTOR
shift # past argument
shift # past value
;;
# actor overrides
-e|--eye)
EYE=$2
shift # past argument
shift # past value
;;
-m|--mouth)
MOUTH=$2
shift # past argument
shift # past value
;;
-cl|--cheek-left)
CHEEK_LEFT=$2
shift # past argument
shift # past value
;;
-cr|--cheek-right)
CHEEK_RIGHT=$2
shift # past argument
shift # past value
;;
-a|--arm)
FLIP_ARM=$2
shift # past argument
shift # past value
;;
-M|--motion)
MOTION=$2
shift # past argument
shift # past value
;;
# table configuration
-t|--table-length)
TABLE_LENGTH=$2
shift # past argument
shift # past value
;;
# sceens
-s|--scan)
SCENE=$SCAN
shift # past argument
;;
-c|--clear)
SCENE=$CLEAR
shift # past argument
;;
# uh-oh
*)
echo Unknown argument: $1
exit 1
;;
esac
done
#####################################
# FACES
#####################################
face="$CHEEK_LEFT$EYE$MOUTH$EYE$CHEEK_RIGHT"
faceRight="$CHEEK_LEFT ${face:${#CHEEK_LEFT}}"
faceLeft="${face%%$CHEEK_RIGHT*} $CHEEK_RIGHT"
putWord $CHEEK_LEFT
CHEEK_LEFT_WIDTH=$(getWidth)
putWord $EYE
EYE_WIDTH=$(getWidth)
putWord "$MOUTH"
MOUTH_WIDTH=$(getWidth)
putWord "$CHEEK_RIGHT"
CHEEK_RIGHT_WIDTH=$(getWidth)
putWord "$faceRight"
FACE_RIGHT_WIDTH=$(getWidth)
putWord "$faceLeft"
FACE_LEFT_WIDTH=$(getWidth)
#####################################
# TABLE
#####################################
tableLength=${TABLE_LENGTH:-"1"}
tableEndChar="┬"
tableMiddleChar="─"
tableMiddle=$(makeString "$tableMiddleChar" $tableLength)
table="$tableEndChar$tableMiddle$tableEndChar"
putWord "$table"
TABLE_WIDTH=$(getWidth)
flippedTableEndChar="┻"
flippedTableMiddleChar="─"
flippedTableMiddle=$(makeString "$flippedTableMiddleChar" $tableLength)
tableFlipped="$flippedTableEndChar$flippedTableMiddle$flippedTableEndChar"
putWord "$tableFlipped"
FLIPPED_TABLE_WIDTH=$(getWidth)
putWord "$MOTION"
MOTION_WIDTH=$(getWidth)
faceRightArms="$CHEEK_LEFT$FLIP_ARM${face:${#CHEEK_LEFT}} $FLIP_ARM"
putWord "$faceRightArms"
FACE_RIGHT_ARMS_WIDTH=$(getWidth)
flipTableRight="$faceRightArms$MOTION$tableFlipped"
putWord "$flipTableRight"
FLIP_TABLE_RIGHT_WIDTH=$(getWidth)
#####################################
# DEFAULTS / GLOBALS
#####################################
cols=$(tput cols)
rows=$(tput lines)
middleY=$(($rows / 2))
middleX=$(($cols / 2))
fps=${fps:-"60"}
delay=$(divide 1 $fps)
#####################################
# HELP
#####################################
function help() {
local width=77
local line=$(repeat "#" $width)
local fname="# table.sh"
local spacer=$(repeat " " $((width - FLIP_TABLE_RIGHT_WIDTH - ${#fname})))
cat << EOF
$line
$fname$spacer$flipTableRight
$line
Everybody's favorite table flipper, lightly animated
Options:
-f, --fps [int] change the running speed (default 60)
-h, --help display this help
Actors:
--actor [actor] choose a specific actor
actors: $(echo "$actors" | sed 's/ /, /g')
Actor Options:
-e, --eye [char(s)] customize flip's eyes
-m, --mouth [char(s)] customize flip's mouth
-cl, --cheek-left [char(s)] customize flip's left cheek
-cr, --cheek-right [char(s)] customize flip's right cheek
-a, --arm [char(s)] customize flip's flippin' arms
-M, --motion [char(s)] customize the table flippin' motion
Table Options:
-t, --table-length [int] change the length of the table
Scenes:
-s, --scan like a printer...but not really
-c, --clear like shell clear, but better (worse)
EOF
}
#####################################
# MOVEMENT
#####################################
function enterLeft() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
for ((i=0; i < $((${#actor} - 1)); i++)); do
tput cup $y 0
echo "${actor: ((0 - i - 1))}"
sleep $waitFor
done
}
function moveRight() {
local actor="$1"
local from=${2:-"0"}
local to=${3:-"$cols"}
local y=${4:-"$middleY"}
local waitFor=${5:-"$delay"}
local x="$from"
putWord $actor
local actorWidth=$(getWidth)
deleteWord $actorWidth
local toActual=$((to - actorWidth))
local spacer=""
while [ "$x" -le "$toActual" ]; do
if [ "$x" -eq "0" ]; then
tput cup $y $x
else
tput cup $y $((x - 1))
spacer=" "
fi
echo "$spacer$actor"
((x++))
sleep $waitFor
done
}
function exitRight() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
local length=${#actor}
local firstChar="${actor:0:1}"
local squeegie=$(empty "$CHEEK_LEFT_WIDTH")
local extraSpaces=$(extraDisplayChars "$actor")
for ((i=0; i < $length; i++)); do
actor="${actor:0:$((length - i - 1))}"
local toDraw="$squeegie$actor"
tput cup $y $((cols - ${#toDraw} - $extraSpaces))
echo "$toDraw"
sleep $waitFor
done
}
function scanRight() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
enterLeft "$actor" $y $waitFor
moveRight "$actor" 0 $cols $y $waitFor
exitRight "$actor" $y $waitFor
}
function enterRight() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
local length=${#actor}
for ((i=1; i < $length; i++)); do
local partial="${actor:0:$i}"
putWord $partial
local partialWidth=$(getWidth)
deleteWord $partialWidth
tput cup $y $((cols - partialWidth)))
echo "$partial"
sleep $waitFor
done
}
function moveLeft() {
local actor="$1"
putWord $actor
local actorWidth=$(getWidth)
deleteWord $actorWidth
local from=${2:-$((cols - actorWidth))}
local to=${3:-"0"}
local y=${4:-"$middleY"}
local waitFor=${5:-"$delay"}
local x="$from"
local space=""
while [ "$x" -ge "$to" ]; do
tput cup $y $x
echo "$actor$space"
space=" "
((x--))
sleep $waitFor
done
}
function exitLeft() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
local lastChar="${actor: -1}"
local squeegie=$(empty "$CHEEK_RIGHT_WIDTH")
local length=${#actor}
for ((i = 0; i < "$length"; i++)); do
tput cup $y 0
actor="${actor:1}$squeegie"
echo "$actor"
sleep $waitFor
done
}
function scanLeft() {
local actor="$1"
local y=${2:-"$middleY"}
local waitFor=${3:-"$delay"}
enterRight "$actor" $y $waitFor
putWord $actor
local actorWidth=$(getWidth)
deleteWord $actorWidth
moveLeft "$actor" $((cols - actorWidth)) 0 $y $waitFor
exitLeft "$actor" $y $waitFor
}
#####################################
# SCENES
#####################################
function scan() {
local actorRight=${1:-"$faceRight"}
local actorLeft=${2:-"$faceLeft"}
local waitFor=${3:-"$delay"}
local row=0
while [ "$row" -le "$rows" ]; do
if [ $((row % 2)) -eq "0" ]; then
scanRight "$actorRight" $row $waitFor
else
scanLeft "$actorLeft" $row $waitFor
fi
((row++))
sleep $waitFor
done
}
function flip() {
getCenterStart $table
local tableStart=$(getCenterStart "$table")
local eyeSleep=1
tput cup $middleY $tableStart
echo $table
enterLeft "$faceRight"
moveRight "$faceRight" 0 $tableStart
local actorStart=$((tableStart - FACE_RIGHT_WIDTH))
sleep $eyeSleep
tput cup $middleY $actorStart
echo $faceLeft
sleep $eyeSleep
tput cup $middleY $actorStart
echo $faceRight
sleep $eyeSleep
tput cup $middleY $actorStart
echo $flipTableRight
sleep 0.1
tput cup $middleY $((actorStart + FACE_RIGHT_ARMS_WIDTH))
empty "$MOTION_WIDTH"
sleep 0.5
tput cup $middleY $actorStart
empty "$FACE_RIGHT_ARMS_WIDTH"
moveLeft "$faceLeft" $actorStart 0
exitLeft "$faceLeft"
}
#####################################
# MAIN
#####################################
# setup
case $SCENE in
$HELP)
help
;;
$SCAN|$CLEAR)
scan
;;
$FLIP)
flip
;;
*)
echo Unknown scene: $SCENE
exit 1
;;
esac