summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-12-27 03:30:51 +0000
committerdavidovski <david@davidovski.xyz>2023-12-27 03:30:51 +0000
commit0c38c59dd225ffaa5005d587881cc8348a39251c (patch)
tree34e3f5aaf0fb1af41cabf20807cfa6d53eabf05d
initial commit
-rw-r--r--README.md3
-rwxr-xr-xtypr.sh83
2 files changed, 86 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c05cf28
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# typr
+
+a typing test for the terminal written in shell
diff --git a/typr.sh b/typr.sh
new file mode 100755
index 0000000..7177153
--- /dev/null
+++ b/typr.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+entered_text=""
+text="the quick brown fox jumps over the lazy dog"
+
+tty_init () {
+ tput clear
+ export SAVED_TTY_SETTINGS=$(stty -g)
+ stty raw -echo
+ trap typr_cleanup 1 2 3 6
+
+ printf "[3 q"
+
+}
+
+tty_cleanup () {
+ tput clear
+ stty $SAVED_TTY_SETTINGS
+}
+
+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"
+}
+
+typr_draw_text () {
+ tput civis
+ tput clear
+ printf ""
+
+ t="$text"
+ e="$entered_text"
+ while [ "$t" ] ; do
+ ct=${t%${t#?}}
+ ce=${e%${e#?}}
+ t="${t#?}"
+ e="${e#?}"
+
+ [ "$ct" = "$ce" ] \
+ && printf "" \
+ || {
+ [ "$e" ] \
+ && printf "" \
+ || printf ""
+ }
+
+ printf "$ct"
+ done
+ printf "[1;$((${#entered_text} + 1))H"
+ tput cnorm
+}
+
+
+typr_main () {
+ while true; do
+ typr_draw_text
+ c="$(tty_readc)"
+ case "$c" in
+ ' ') break;;
+ '')
+ entered_text="${entered_text%?}"
+ ;;
+ *)
+ echo "$c" >> LOG
+ entered_text="$entered_text$c"
+ ;;
+
+ esac
+ done
+}
+
+typr_init () {
+ tty_init
+ typr_main
+ tty_cleanup
+}
+
+typr_init