dictionary.sh (2691B)
1 #!/bin/sh 2 3 flat=0 4 inverse=0 5 random=0 6 all=0 7 8 OPTS="h:ira" 9 LONGOPTS="help,inverse,random,all" 10 11 parsed=$(getopt --options=$OPTS --longoptions=$LONGOPTS -- "$@") 12 eval set -- "${parsed[@]}" 13 14 while true; do 15 case "$1" in 16 -i|--inverse) inverse=1 ;; 17 -r|--random) random=1 ;; 18 -a|--all) all=1 ;; 19 20 --) # end of arguments 21 shift 22 break 23 ;; 24 25 *) 26 printf '%sn' "Error while parsing CLI options" 1>&2 27 ;; 28 esac 29 30 shift 31 done 32 33 while true; do 34 row=$(psql spanish-dictionary -q -c "SELECT word_english, word_spanish, entry_id, successes FROM dictionary WHERE next_appearance < current_timestamp ORDER BY random() limit 1;" --csv -t) 35 row=$(echo $row | perl -pe 's/,(?=[^s])/%/g' | sed 's/"//g') 36 37 if [ "$row" == "" ]; then 38 next_appearance=$(psql spanish-dictionary -q -c "SELECT next_appearance FROM dictionary ORDER BY next_appearance limit 1;" --csv -t) 39 echo "Up to date! next question is at $next_appearance." 40 all=0 41 else 42 43 #word_english=$(echo $row | awk 'FNR == 1 { print };') 44 #word_spanish=$(echo $row | awk 'FNR == 2 { print };') 45 #entry_id=$(echo $row | awk 'FNR == 3 { print };') 46 #successes=$(echo $row | awk 'FNR == 4 { print };') 47 48 word_english=$(echo $row | awk '{split($0,a,"%"); print a[1]};') 49 word_spanish=$(echo $row | awk '{split($0,a,"%"); print a[2]};') 50 entry_id=$(echo $row | awk '{split($0,a,"%"); print a[3]};') 51 successes=$(echo $row | awk '{split($0,a,"%"); print a[4]};') 52 53 if [ "$random" = "1" ]; then 54 choice=$(shuf -i 1-2 -n 1) 55 else 56 choice=$inverse 57 fi 58 59 if [ "$choice" == "1" ]; then 60 var=$word_english 61 echo $word_spanish 62 else 63 var=$word_spanish 64 echo $word_english 65 fi 66 67 read user_input 68 69 if [ "$var" == "$user_input" ]; then 70 successes=$((successes + 1)) 71 fib=$(bc -l <<< "(((sqrt(5) + 1) / 2) ^ ($successes + 1) - (-((sqrt(5) + 1) / 2)) ^ -($successes + 1)) / sqrt(5)" | awk '{printf("%dn", ($1 + 0.5) * 5)}') 72 73 echo "correct! will ask you again in $fib minutes." 74 future_date=$(date --iso-8601=seconds -d "+$fib minutes") 75 psql spanish-dictionary -q -c "UPDATE dictionary SET successes = $successes, next_appearance = '$future_date' WHERE entry_id = $entry_id" 76 else 77 actual="" 78 for (( j=0; j<${#var}; j++ )); do 79 if [ "${var:$j:1}" == "${user_input:$j:1}" ]; then 80 actual="$actual${var:$j:1}" 81 else 82 actual="$actuale[01;31m${var:$j:1}e[0m" 83 fi 84 done 85 echo -e $actual 86 psql spanish-dictionary -q -c "UPDATE dictionary SET successes = 0 WHERE entry_id = $entry_id" 87 fi 88 fi 89 90 if [ "$all" == "0" ]; then 91 break 92 fi 93 done