summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-12-27 17:22:43 +0000
committerdavidovski <david@davidovski.xyz>2023-12-27 17:22:43 +0000
commit806aaf2b6adabe2f29a50b402d354357214fb30a (patch)
treec717908915e958314db72adba7dcdb200007087a
parent6df082d5c5ed31307db0f8e5dac01d95915d4a35 (diff)
show time during typing
-rwxr-xr-xtypr.sh79
1 files changed, 63 insertions, 16 deletions
diff --git a/typr.sh b/typr.sh
index fcfa0f9..9e9d510 100755
--- a/typr.sh
+++ b/typr.sh
@@ -29,20 +29,12 @@ tty_readc () {
}
typr_draw_text () {
-
- cols="$(tput cols)"
- lines="$(tput lines)"
-
- startcol=$((cols / 3))
- line=$((lines / 3))
- cpos="0;0"
-
color=""
-
- tput civis
+ line=${areay}
+ startcol=${areax}
cpos="${line};${startcol}"
- printf "[${cpos}H${color}"
+ draw="[${cpos}H${color}"
i=0
t="$text"
@@ -62,27 +54,57 @@ typr_draw_text () {
[ "$color" != "$newcolor" ] && {
color="$newcolor"
- printf "$newcolor"
+ draw="${draw}$newcolor"
[ "$color" = "" ] && cpos="${line};$((i+$startcol))"
}
[ "$i" -gt "$startcol" ] && {
line=$((line+1))
- printf "[${line};${startcol}H"
+ draw="${draw}[${line};${startcol}H"
i=0
}
[ "$ct" = " " ] && [ "$color" = "" ] \
- && printf "_" \
- || printf "$ct"
+ && draw="${draw}_" \
+ || draw="$draw$ct"
i=$((i+1))
done
- printf "[${cpos}H"
+ printf "%s[${cpos}H" "$draw"
tput cnorm
}
+typr_get_time () {
+ now="$(date +%s%N)"
+ time_ns=$((now-start))
+
+ 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 () {
+ printf "[$((areay-1));${areax}H%s" "$(typr_get_time)"
+}
+
+typr_show_results () {
+ now="$(date +%s%N)"
+ time_ns=$((now-start))
+
+ time_ms=$(((time_ns/1000000)%1000))
+ time_seconds=$(((time_ns/1000000000)%60))
+ time_minutes=$((time_ns/60000000000))
+
+ words=$(set -- $text; printf "%s\n" "$#")
+ wpm="$((time_ns*words/60000000000))"
+
+ printf "[$((areay-1));${areax}Htime: %02d:%02d.%03d\twpm: %s" "$time_minutes" "$time_seconds" "$time_ms" "$wpm"
+
+}
+
typr_generate_text () {
words="$(printf "%s " $words | shuf)"
wordcount=100
@@ -100,7 +122,19 @@ typr_generate_text () {
}
+typr_draw_loop () {
+ while true; do
+ typr_draw_time
+ done
+}
+
+
typr_main () {
+ start="$(date +%s%N)"
+
+ typr_draw_loop &
+ draw_pid="$!"
+
while true; do
typr_draw_text
c="$(tty_readc)"
@@ -113,12 +147,25 @@ typr_main () {
echo "$c" >> LOG
entered_text="$entered_text$c"
;;
+ esac
+ [ "${#entered_text}" = "${#text}" ] && break
+ done
+ kill "$draw_pid"
+
+ while true; do
+ case "$(tty_readc)" in
+ ' '|'') break;;
esac
done
}
typr_init () {
+ cols="$(tput cols)"
+ lines="$(tput lines)"
+
+ areax=$((cols / 3))
+ areay=$((lines / 3))
tty_init
typr_generate_text
typr_main