-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper
executable file
·86 lines (82 loc) · 2.33 KB
/
typer
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
#!/usr/bin/env bash
# typer - pipe in, type out
# typer © Mike Lalumiere 2019
typer() {
# usage
read -r -d '' use <<EOF
USAGE: typer [OPTIONS] < TEXT
Write text input delayed to simulate typing.
OPTIONS:
-h : Print some help text
-c RATE : Print rate in characters per second (0-999)
-r : Randomized delay between characters (default)
-s : Smooth output, no randomization
EXAMPLES:
$> typer -c 5 <<< "Hello, \$USER..."
$> typer -r -c 50 < <(fortune)
$> nethack | typer -c 600
$> ttyrec /dev/null | typer -s -c 900
(c) Mike Lalumiere 2019
EOF
# return nicely on interrupt
trap 'printf "\n"; return 2' SIGINT
# get options
local rate=30 mode="random"
local opt OPTIND OPTARG
while getopts ":hc:qrs" opt; do
case "$opt" in
c) # rate in characters per second 0-999
rate="$OPTARG"
if [[ "$rate" -ge 1000 ]] || [[ "$rate" -le 0 ]]; then
printf "Rate must be between 1 and 999.\\n" >&2
return 2
fi
;;
r) # random mode
mode="random"
;;
s) # smooth mode
mode="smooth"
;;
q) # quick mode
mode="quick"
;;
h) # help?
printf "%s\\n" "$use"
return 1
;;
\?) # invalid
printf "Invalid option: -%s\\n\\n%s\\n" "$OPTARG" "$use" >&2
return 1
;;
esac
done
# speed things up
if (( rate < 5 )); then
rate=0
fi
# read one character at a time from standard input
local delay=0
while IFS= read -r -N 1 c; do
# calculate delay in thousanths of a second
case "$mode" in
smooth)
delay=$(printf "0.%03d" $(( 1000 / rate )) )
;;
random)
delay=$(printf "0.%03d" $(( RANDOM % ( 2000 / rate ) )) )
;;
quick)
delay=0
;;
esac
# delay that long
if [ "$delay" != "0.000" ]; then
sleep "$delay"
fi
# write the character
printf "%s" "$c"
done
}
# run it unless sourced
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then typer "$@"; fi