-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper
executable file
·161 lines (140 loc) · 4.15 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
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
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/typer
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/typer
## https://davoudarsalani.ir
source "$HOME"/main/scripts/gb
source "$HOME"/main/scripts/gb-audio
source "$HOME"/main/scripts/gb-calculation
title="${0##*/}"
function display_help { ## {{{
source "$HOME"/main/scripts/.help
typer_help
}
## }}}
function countdown { ## {{{
[ "$no_countdown" ] && return
local int
for ((int=10; int>0; int--)); {
printf '%s\e[0K\r' "$(blue "$int")"
sleep 1
}
}
## }}}
function sleep_now { ## {{{
if [ "$interval" == 'random' ]; then
sleep "${intervals["$(( "$RANDOM" % "$intervals_length" ))"]}"
else
sleep "$interval"
fi
}
## }}}
function play_random_key_sound { ## {{{
pacmd play-file "$key_sound" "${key_sounds["$(( "$RANDOM" % "$key_sounds_length" ))"]}" "$def_sink_index"
}
## }}}
function type_in_vim { ## {{{
local chars_count char_idx perc remaining_chars eta_mid eta_mid_conv
chars_count="$(wc -c < "$file")"
# (( chars_count+=100 )) ## add 100 just in case
countdown
## strike keys
for ((char_idx=1; char_idx<="$chars_count"; char_idx++)); {
(( perc="char_idx * 100 / chars_count" ))
(( remaining_chars="chars_count - char_idx" ))
eta_mid="$(float_pad "${remaining_chars}*${intervals_middle_member}")"
eta_mid_conv="$(convert_second "$eta_mid")"
printf '%s %s %s\e[0K\r' "$(green "${char_idx}/${chars_count}")" "$(yellow "${perc}%")" "$(brown "eta: ${eta_mid_conv}")"
xdotool key c
"$play" && sleep_now
}
printf '\n'
accomplished 'Done. Press Ctrl+C in vim to exit'
}
## }}}
function type_in_terminal { ## {{{ by Metalx1000 (https://pastebin.com/Uqf6DPmZ)
local lines_count line_idx
countdown
clear
while read line; do ## read file one line at a time
lines_count="${#line}"
for ((line_idx=0; line_idx<"$lines_count"; line_idx++)); { ## read line one character at a time
printf '%s' "${line:$line_idx:1}" ## NOTE no \n
"$play" && sleep_now
}
printf '\n'
done < "$file"
}
## }}}
function prompt { ## {{{
for _ in "$@"; {
case "$1" in
-f )
file="${file:-"$(get_input 'file')"}" ;;
-i )
interval="${interval:-"$(get_input 'interval (e.g. 0.2, 1, random, etc)')"}" ;;
esac
shift
}
}
## }}}
function get_opt { ## {{{
local options
options="$(getopt --longoptions 'help,quiet,no-countdown,file:,interval:' --options 'hqnf:i:' --alternative -- "$@")"
eval set -- "$options"
while true; do
case "$1" in
-h|--help )
display_help ;;
-q|--quiet )
is_quiet='true' ;;
-n|--no-countdown )
no_countdown='true' ;;
-f|--file )
shift
file="$1" ;;
-i|--interval )
shift
interval="$1" ;;
-- )
break ;;
esac
shift
done
}
## }}}
get_opt "$@"
IFS= ## prevent leading spaces from being ignored (https://stackoverflow.com/questions/29689172/bash-read-ignores-leading-spaces)
intervals=(
0.05
0.06
0.07
0.08
0.09
0.1
0.2
0.3
)
intervals_length="${#intervals[@]}"
intervals_middle_member="${intervals["$(( "$intervals_length" / 2 ))"]}"
key_sounds=(
"$HOME"/main/linux/sounds/key_press_1.wav
"$HOME"/main/linux/sounds/key_press_1_dance.wav
"$HOME"/main/linux/sounds/key_press_2.wav
"$HOME"/main/linux/sounds/key_press_2_fullbase.wav
)
key_sounds_length="${#key_sounds[@]}"
[ "$is_quiet" == 'true' ] && play=':' || play='play_random_key_sound'
heading "$title"
main_item="$(pipe_to_fzf 'in vim' 'in terminal' 'help')" && wrap_fzf_choice "$main_item" || exit 37
case "$main_item" in
'in vim' )
prompt -f -i
type_in_vim ;;
'in terminal' )
prompt -f -i
type_in_terminal ;;
help )
display_help ;;
esac