summaryrefslogtreecommitdiff
path: root/typr.sh
blob: 4c46d12a0dc36a21f65ae8ad60a5b3469a50ea34 (plain)
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
#!/bin/sh
set -f

words="the be of and a to in he have it that for they i with as not on she at by this we you do but from or which one would all will there say who make when can more if no man out other so what time up go about than into could state only new year some take come these know see use get like then first any work now may such give over think most even find day also after way many must look before great back through long where much should well people down own just because good each those feel seem how high too place little world very still nation hand old life tell write become here show house both between need mean call develop under last right move thing general school never same another begin while number part turn real leave might want point form off child few small since against ask late home interest large person end open public follow during present without again hold govern around possible head consider word program problem however lead system set order eye plan run keep face fact group play stand increase early course change help line"

text=""

test_type="time"
test_length="30"

# test if date supports %N
NANOSECONDS=true
_s=
case "$(date +%N)" in 
    ""|"%N") NANOSECONDS=false;;
esac

gettime () {
    $NANOSECONDS && date +%s%N \
        || printf "%s000000000" "$(date +%s)" 2>/dev/null
}

tty_init () {
    printf ""
    export SAVED_TTY_SETTINGS=$(stty -g)
    stty raw -echo
    trap typr_cleanup 1 2 3 6

    printf "[6 q"
}

tty_cleanup () {
    stty $SAVED_TTY_SETTINGS
    printf "[?25h"
}

tty_readc () {
    stty -echo -icanon min 1 time 0
    s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)"
    stty -icanon min 0 time 0
    [ "$s" = "" ]  && {
        s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)"
    }
    printf "%s" "$s"
}

typr_draw_text () {
    y="${areay}" startcol=${areax}

    printf "[?25l"
    printf "%s\n" "$text" | while IFS= read -r line; do
        printf "%s" "[${y};${startcol}H${line}"
        y=$((y+1))
    done
    printf "[%d;%dH[?25h" "${areay}" "${startcol}"

}

typr_get_time () {
    time_ns="$1"

    time_ms=$(((time_ns/1000000)%1000))
    time_seconds=$(((time_ns/1000000000)%60))
    time_minutes=$((time_ns/60000000000))

    printf "%02d:%02d.%03d" "$time_minutes" "$time_seconds" "$time_ms"
}

typr_draw_time () {
    [ -z "$start" ] && return 1

    now="$(gettime)"
    time_ns=$((now-start))
    case "$test_type" in
        "time")
            time_ns=$((1000000000*test_length - time_ns))
            ;;
    esac

    [ "$time_ns" -lt "0" ] && {
        [ "$(((time_ns / 500000000) % 2))" = "0" ] \
            && printf "[$((areay - 1));${areax}H%s" "         " \
            && return 1
        time_ns=0
    }

    printf "[$((areay - 1));${areax}H%s" "$(typr_get_time "$time_ns")"
}

typr_calculate_raw_wpm () {
    time_ns="$1"
    printf "%s" "$((${#entered_text}*60000000000/(time_ns*5)))"
}

typr_calculate_acc () {
    [ "$total_kp" != "0" ] && printf "%s" "$(((100*correct_kp)/total_kp))" || printf "%s" "0"
}


typr_show_results () {
    printf "[?25l"

    now="$(gettime)"
    time_ns=$((now-start))

    #wc="$(set -- $text ; printf "%s" "$#")"
    raw_wpm="$(typr_calculate_raw_wpm "$time_ns")"
    acc="$(typr_calculate_acc)"
    wpm="$(((acc*raw_wpm) / 100))"

    printf "[%s;${areax}H%s" \
        "${areay}" "wpm" \
        "$((areay+1))" "$wpm" \
        "$((areay+2))" "acc" \
        "$((areay+3))" "${acc}%" \
        "$((areay+4))" "time" \
        "$((areay+5))" "$(typr_get_time "$time_ns")" \
        "$((areay+6))" "raw" \
        "$((areay+7))" "$raw_wpm"
}

typr_wrap_text () {
    t="$text" line="" lt=""
    while [ "$t" ] ; do
        lt="$ct"
        ct="${t%"${t#?}"}"
        t="${t#?}"

        [ "$lt" = " " ] && {
            next_word=${t%% *}
            [ "${#line}" -gt "$((areax - ${#next_word}))" ] && {
                printf "%s\n" "${line}"
                line=""
            }
        }
        line="${line}${ct}"
    done
    printf "%s\n" "${line}"
}

typr_generate_text () {
    case "$test_type" in
        "words")
            wordcount="$test_length"
            ;;
        "time")
            # TODO auto add lines as test progresses
            wordcount="100"
            ;;
    esac

    text="$(printf "%s " $(printf "%s\n" $words | shuf -n $wordcount))"
    text="${text% }"
    text="$(typr_wrap_text)"
}

typr_draw_loop () {
    while true; do
        typr_draw_time
    done
}

typr_start_timer () {
    start="$(gettime)"
    typr_draw_loop &
    draw_pid="$!"
    export start draw_pid
}

typr_update_acc () {
    t="$current_line" e="$entered_line"
    while [ "$e" ] ; do
        ct="${t%"${t#?}"}"
        t="${t#?}"

        ce="${e%"${e#?}"}"
        e="${e#?}"

        [ "$ct" = "$ce" ] && correct_kp=$((correct_kp+1))
        total_kp=$((total_kp+1))
    done
    export correct_kp total_kp
}

typr_redraw_line () {
    color=""
    line=$((areay + current_line_no - 1))
    startcol=${areax}

    draw="[${line};${startcol}H${color}"

    i=0
    t="$current_line"
    e="$entered_line"
    while [ "$t" ] ; do
        ct="${t%"${t#?}"}"
        t="${t#?}"

        ce="${e%"${e#?}"}"
        e="${e#?}"

        case "$ce" in
            "$ct")
                newcolor=""
                ;;
            "")
                newcolor=""
                ;;
            *)
                newcolor=""
            ;;
        esac

        [ "$color" != "$newcolor" ] && {
            color="$newcolor"
            draw="${draw}$newcolor"
        }


        [ "$color" = "" ] && {
            case "$ce" in " ") draw="${draw}_" ;; *) draw="${draw}$ce"; esac
        } || {
            draw="${draw}$ct"
        }
    done
    printf "%s[${line};$((${#entered_line} + startcol))H[?25h" "$draw"
}

typr_new_line () {
    [ -n  "$entered_text" ] && entered_text="$entered_text
"
    entered_text="${entered_text}${entered_line}"
    typr_update_acc

    entered_line=""

    current_line_no=$((current_line_no+1))
    current_line="$(typr_get_text_line $current_line_no)"
    typr_redraw_line
}

typr_add_letter () {
    c="$1"
    [ -z "$start" ] && typr_start_timer

    entered_line="$entered_line$c"

    typr_redraw_line

    [ "${#entered_line}" = "${#current_line}" ] && {
        typr_new_line
    }

}

typr_del_letter () {
    [ -z "$entered_line" ] && prev=true || prev=false

    entered_line="${entered_line%?}"
    typr_redraw_line

    $prev && [ "$current_line_no" -gt "1" ] || return 0

    # move back one line in entered
    current_line_no=$((current_line_no-1))
    current_line="$(typr_get_text_line $current_line_no)"

    entered_line="$(typr_get_text_line $current_line_no "$entered_text")"
    entered_line="${entered_line%?}"
    entered_text="$(typr_remove_last_line "$entered_text")"
    typr_redraw_line
}

# removes the last line, ignoring trailing whitespace
#
typr_remove_last_line() {
    t="${1%
}"
    printf "%s" "${1%
}" | while IFS= read -r line; do
        printf "%s\n" "$line"
    done
}

typr_get_text_line() {
    n="$1"
    t="${2:-$text}"
    i=0
    case "$n" in
        ""|*[!0-9]*) return 1;;
    esac

    printf "%s\n" "$t" | while IFS= read -r line; do
        i=$((i+1))
        [ "$i" = "$n" ] && printf "%s\n" "$line"
    done
}

typr_main () {
    entered_text=""
    entered_line=""
    start=

    total_kp=0
    correct_kp=0

    current_line_no=1
    current_line="$(typr_get_text_line $current_line_no)"

    export correct_kp total_kp entered_text entered_line start current_line_no current_line

    printf ""
    typr_draw_text
    while true; do
        c="$(tty_readc)"
        case "$c" in
            ''|'') break;;
            '')
                typr_del_letter
                ;;
                " "|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9|,|.|\?|\"|\'|!|-)
                    typr_add_letter "$c"
                ;;
            '	')
                kill "$draw_pid"
                typr_generate_text
                typr_main
                return
                ;;
        esac

        case "$test_type" in
            "words")
                [ "$((${#entered_text}+${#entered_line}))" = "${#text}" ] && break
                ;;
            "time")
                now="$(gettime)"
                time_ns=$((now-start))
                [ "$time_ns" -gt "$((test_length*1000000000))" ] && break
                ;;
        esac
    done
    kill "$draw_pid"

    entered_line="$entered_line "
    typr_new_line
    typr_show_results

    while true; do
        case "$(tty_readc)" in
            ''|''|q) break;;
            '	')
                typr_generate_text
                typr_main
                return
                ;;
        esac
    done
}

typr_init () {
    set -- $(stty size)
    cols="$2"
    lines="$1"

    areax=$((cols / 3))
    areay=$((lines / 3))
    tty_init
    typr_generate_text
    typr_main
    tty_cleanup
}

typr_usage () {
    printf "%s\n" "$0 [-h] [-m time|words] [-d duration/words]" 1>&2
    exit 1
}


while getopts "m:d:h" opt; do
    case "$opt" in
        h)
            typr_usage ;;
        m)
            case "$OPTARG" in
                "time"|"words") test_type="$OPTARG" ;;
                *) typr_usage ;;
            esac
        ;;
        d)
            test_length="$OPTARG"
        ;;
    esac
done

shift $((OPTIND-1))

typr_init