INDEX
2024-03-13 16:40 - Typing test I wrote a basic typing test in bash to trial out these backslash string commands stty sane # Do this first of all, or you'll find unexpected issues with alignment # Just add these in a string with "echo -en" and it does as you'd expect # Although bare in mind inputing a character inherently moves you right GO_UP="\033[1A"; GO_DOWN="\033[1B"; GO_LEFT="\033[1D"; GO_RIGHT="\033[1C" BOLD_ON="\e[1m"; BOLD_OFF="\e[0m" TXT_BLACK="\033[30m"; BG_PLAIN="\033[0m"; BG_TRUE="\033[44m"; BG_FALSE="\033[41m" So how do you actually make a typing test from this? # Get a list of N random words, in a line, with no spacing at the end # Will later make this read from a file and use "fold" to word wrap it grep -v "'" /usr/share/dict/british | shuf -n${textWords} | tr '\n' ' ' | sed 's/.\{1\}$// time0=$(date +%s.%N); typingTest; time1=$(date +%s.%N) # Track timing of test read -rsn1 char # Read a single character, with no output charExp="${text:pos-1:1}" # Get the expected character at that position in the text # Have a line of text, gray=unchecked, blue=correct, red=incorrect # Cursor moves along this line of text and overwrites formatting based on conditionals echo -en "${TXT_BLACK}"; echo -en "${BG_TRUE}${charExp}"; echo -en "${BG_PLAIN}" # Want to be able to edit so test for backspace characters [[ $char == $'\b' ]] || [[ $char == $'\177' ]] echo -en "${GO_LEFT}${charExp}${GO_LEFT}" # Move cursor back and add in unformatted character # Using a "scoreTrack" to monitor accuracy - 1=correct 0=false [WORD -> WARD = 1011] score=`grep -o "1" <<< "$scoreTrack" | wc -l` # Count how many 1s are present finalAccuracy=`echo "(${score}/${textLen}) * 100" | bc` # Get % scale of accuracy # Check the timing (I think this is vaguely wrong anyway) finalSpeed=`echo "scale=2; ( ${textWords}/(${time1} - ${time0}) )*60*2" | bc` Next plan is add mutliline tests, allow piped input and improve progressive timing system Also add live metrics (clock), add different tests, add different views (sliding text?) Might be worth making a marquee in bash too
2024-03-18 14:00 - Early version 2 Replaced the system of random words with an option to read a file # A nice system for looping through arguments and assigning their contents without shifts for (( i=1; i <= "$#"; i++ )); do case ${!i} in -f|--file) ((i++)); textfile="${!i}" ;; esac done # Read a file and filter it into just alphanumeric words text=`cat "$1" | tr -d '\n\t' | tr -cd '[:space:][:alnum:]' | tr -s '[:space:]'` # Fold the text based on dimensions of the screen function foldtext() { # "tput cols" gets the number of columns on the terminal winWidth=`tput cols` # New lines -> Spaces. Remove duplicate spaces. Word wrap around current terminal width text=`echo "$text" | tr '\n' ' ' | tr -s ' ' | fold -s -w "$winWidth"`; } From this I just need to integrate in the typing test feature and some scrolling/line position Should otherwise be the same system entirely.