summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-12-30 18:53:32 +0000
committerdavidovski <david@davidovski.xyz>2023-12-30 18:53:32 +0000
commit8532f9cfd4be9b8b8591f0a2c975d64201f296bc (patch)
treeb46f8ef9ad8029260e17298db2e14732db3b77a5
parentc699b3751e1c3aa5407ec78854e8a7bc5f3eca0e (diff)
support systems that do not have %N in date0.4
-rwxr-xr-xtypr.sh22
1 files changed, 17 insertions, 5 deletions
diff --git a/typr.sh b/typr.sh
index 8c5c4ec..36efb15 100755
--- a/typr.sh
+++ b/typr.sh
@@ -7,6 +7,18 @@ 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)"
+}
+
tty_init () {
printf ""
export SAVED_TTY_SETTINGS=$(stty -g)
@@ -58,7 +70,7 @@ typr_draw_time () {
local now time_ms
[ -z "$start" ] && return 1
- now="$(date +%s%N)"
+ now="$(gettime)"
time_ns=$((now-start))
case "$test_type" in
"time")
@@ -87,7 +99,7 @@ typr_calculate_acc () {
typr_show_results () {
printf "[?25l"
- now="$(date +%s%N)"
+ now="$(gettime)"
time_ns=$((now-start))
wc="$(set -- $text ; printf "%s" "$#")"
@@ -135,7 +147,7 @@ typr_generate_text () {
;;
esac
- text="$(printf "%s " $(printf "%s\n" $words | shuf -r -n $wordcount))"
+ text="$(printf "%s " $(printf "%s\n" $words | shuf -n $wordcount))"
text="${text% }"
text="$(typr_wrap_text)"
}
@@ -147,7 +159,7 @@ typr_draw_loop () {
}
typr_start_timer () {
- start="$(date +%s%N)"
+ start="$(gettime)"
typr_draw_loop &
draw_pid="$!"
export start draw_pid
@@ -318,7 +330,7 @@ typr_main () {
[ "$((${#entered_text}+${#entered_line}+2))" = "${#text}" ] && break
;;
"time")
- now="$(date +%s%N)"
+ now="$(gettime)"
time_ns=$((now-start))
[ "$time_ns" -gt "$((test_length*1000000000))" ] && break
;;