From 739ff05c19c67d27c5790e365b3315da03c58e34 Mon Sep 17 00:00:00 2001 From: davidovski Date: Wed, 27 Dec 2023 18:56:28 +0000 Subject: line wrapping on words --- typr.sh | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/typr.sh b/typr.sh index 800b7f1..9ceafad 100755 --- a/typr.sh +++ b/typr.sh @@ -42,7 +42,9 @@ typr_draw_text () { i=0 t="$text" e="$entered_text" + lt="" while [ "$t" ] ; do + lt=$ct ct=${t%${t#?}} ce=${e%${e#?}} t="${t#?}" @@ -55,6 +57,12 @@ typr_draw_text () { [ ! "$ce" ] && newcolor="" \ + [ "$i" -gt "$startcol" ] && [ "$lt" = " " ] && { + line=$((line+1)) + draw="${draw}[${line};${startcol}H" + i=0 + } + [ "$color" != "$newcolor" ] && { color="$newcolor" draw="${draw}$newcolor" @@ -62,12 +70,6 @@ typr_draw_text () { } - [ "$i" -gt "$startcol" ] && { - line=$((line+1)) - draw="${draw}[${line};${startcol}H" - i=0 - } - [ "$ct" = " " ] && [ "$color" = "" ] \ && draw="${draw}_" \ || draw="$draw$ct" @@ -85,12 +87,12 @@ typr_get_time () { 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)" + [ ! -z "$start" ] && printf "[$((areay-1));${areax}H%s" "$(typr_get_time)" } typr_show_results () { @@ -115,7 +117,7 @@ typr_show_results () { typr_generate_text () { wordcount=100 - text="$(printf "%s\n" $words | shuf -r -n $wordcount | xargs printf "%s ")" + text="$(printf "%s " $(printf "%s\n" $words | shuf -r -n $wordcount))" text="${text% }" } @@ -126,12 +128,14 @@ typr_draw_loop () { done } - -typr_main () { +typr_start_timer () { start="$(date +%s%N)" - typr_draw_loop & draw_pid="$!" + export start draw_pid +} + +typr_main () { while true; do typr_draw_text @@ -142,7 +146,8 @@ typr_main () { entered_text="${entered_text%?}" ;; *) - echo "$c" >> LOG + [ -z "$start" ] && typr_start_timer + entered_text="$entered_text$c" ;; esac -- cgit v1.2.1