From 55da2672077e95cfef581901676f5805b6a46205 Mon Sep 17 00:00:00 2001 From: davidovski Date: Thu, 28 Dec 2023 13:53:56 +0000 Subject: allow time-based tests --- typr.sh | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'typr.sh') diff --git a/typr.sh b/typr.sh index 0f65de9..82678e6 100755 --- a/typr.sh +++ b/typr.sh @@ -4,6 +4,9 @@ words="the be of and a to in he have it that for they i with as not on she at by text="" +test_type="time" +test_length="30" + tty_init () { printf "" export SAVED_TTY_SETTINGS=$(stty -g) @@ -42,8 +45,7 @@ typr_draw_text () { } typr_get_time () { - now="$(date +%s%N)" - time_ns=$((now-start)) + time_ns="$1" time_ms=$(((time_ns/1000000)%1000)) time_seconds=$(((time_ns/1000000000)%60)) @@ -53,7 +55,18 @@ typr_get_time () { } typr_draw_time () { - [ ! -z "$start" ] && printf "[$((areay-1));${areax}H%s" "$(typr_get_time)" + [ -z "$start" ] && return 1 + + now="$(date +%s%N)" + time_ns=$((now-start)) + case "$test_type" in + "time") + time_ns=$((1000000000*$test_length - $time_ns)) + ;; + esac + + draw_time="$(typr_get_time "$time_ns")" + printf "[$((areay - 1));${areax}H%s" "$draw_time" } typr_calculate_raw_wpm () { @@ -82,7 +95,7 @@ typr_show_results () { "$((areay+2))" "acc" \ "$((areay+3))" "${acc}%" \ "$((areay+4))" "time" \ - "$((areay+5))" "$(typr_get_time)" \ + "$((areay+5))" "$(typr_get_time "$time_ns")" \ "$((areay+6))" "raw" \ "$((areay+7))" "$raw_wpm" } @@ -108,7 +121,16 @@ typr_wrap_text () { } typr_generate_text () { - wordcount=100 + 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 -r -n $wordcount))" text="${text% }" text="$(typr_wrap_text)" @@ -287,7 +309,17 @@ typr_main () { return ;; esac - [ "$((${#entered_text}+${#entered_line}+2))" = "${#text}" ] && break + + case "$test_type" in + "words") + [ "$((${#entered_text}+${#entered_line}+2))" = "${#text}" ] && break + ;; + "time") + now="$(date +%s%N)" + time_ns=$((now-start)) + [ "$time_ns" -gt "$((test_length*1000000000))" ] && break + ;; + esac done kill "$draw_pid" -- cgit v1.2.1