-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpm.plugin.zsh
350 lines (298 loc) · 13.5 KB
/
wpm.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# wpm plugin
#
# Typing speed test available within ZSH
#
# See the README for documentation.
# Handle $0 according to the standard:
# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0:#/*}:-$PWD/$0}"
_OMZ_WPM_PLUGIN_DIR="${0:h}"
TYPING_TABLE_WIDTH=100
RESULT_TABLE_WIDTH=50
FILE_SELECTION_TABLE_WIDTH=50
PROMPT_CHAR=">"
HEADER_SEPARATOR_CHAR="═"
DATA_SEPARATOR_CHAR="─"
VERTICAL_BORDER_CHAR="║"
WORD_LIST_FILE_NAME="words_top-250-english-easy.txt"
source "$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/wpm.utils.zsh"
# Starts a typing speed test
#
# wpm_test <seconds>
#
function wpm_test() {
local test_duration=60
local filename=""
# Parse flags using getopts
while getopts "d:f:" opt; do
case $opt in
d) test_duration=$OPTARG ;; # -d for duration
f) filename=$OPTARG ;; # -f for filename
\?) echo "Usage: wpm_test [-d duration] [-f filename]"; return 1 ;; # Handle invalid options
esac
done
# Update UI state
update_state() {
local is_correct="$1"
# Handle word status and re-draw the table
if [[ $is_correct -eq -1 ]]; then
_draw_table "$TYPING_TABLE_WIDTH" "$word_list_top" "$word_list_bottom"
elif [[ -n $is_correct && $current_word_index -eq 1 ]]; then
word_list_top[$current_word_index]=$'\e[47;40m'"${word_list_top[current_word_index]}"$'\e[0m'
_draw_table "$TYPING_TABLE_WIDTH" "$word_list_top" "$word_list_bottom"
elif [[ -n $is_correct && $current_word_index -gt 1 ]]; then
index=$((current_word_index - 1))
word_list_top[$index]=$(printf "%s" "${word_list_top[index]}" | sed 's/\x1b\[[0-9;]*m//g')
if [[ $is_correct -eq 0 ]]; then
word_list_top[$index]=$'\e[32m'"${word_list_top[index]}"$'\e[0m'
elif [[ $is_correct -eq 1 ]]; then
word_list_top[$index]=$'\e[31m'"${word_list_top[index]}"$'\e[0m'
fi
word_list_top[$current_word_index]=$'\e[47;40m'"${word_list_top[current_word_index]}"$'\e[0m'
_draw_table "$TYPING_TABLE_WIDTH" "$word_list_top" "$word_list_bottom"
fi
local term_width=$(tput cols)
local start_col=$(((term_width / 2) - (TYPING_TABLE_WIDTH / 2) - 1))
printf "\r\033[K"
printf "\r$(printf '%*s' "$start_col" "")$PROMPT_CHAR $user_input"
}
# Generate random word from the list
generate_random_word() {
local random_index=$((($(od -An -N2 -i /dev/urandom) % (${#words[@]})) + 1))
printf "%s\n" "${words[$random_index]}"
}
# Generate a list of random words
generate_word_list() {
local count="$1"
local word_list=()
for i in {1..$count}; do
word_list+=("$(generate_random_word)")
done
printf "%s\n" "${word_list[@]}"
}
# Draw file selection menu
list_files() {
local files=()
local numbered_files=()
local index=1
local file
for file in $(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/lists/*.txt; do
files+=("$(basename "$file")")
local filename_option="$(basename "$file")"
numbered_files+=("$(printf "%-5s %*s" "$index." "$((FILE_SELECTION_TABLE_WIDTH - 10))" "$filename_option")")
((index++))
done
_draw_table "$FILE_SELECTION_TABLE_WIDTH" "Word Lists" "═" "${numbered_files[@]}"
local selection
while true; do
local term_width=$(tput cols)
local start_col=$(((term_width / 2) - (FILE_SELECTION_TABLE_WIDTH / 2) - 1))
printf "\r\033[K"
printf "\r$(printf '%*s' "$start_col" "")Select (1-${#files[@]}): "
read selection
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#files[@]}" ]; then
WORD_LIST_FILE_NAME="${files[$selection]}"
break
fi
printf "%s\n" "Invalid selection. Please try again."
done
}
trap 'update_state -1' WINCH # Handle window resize
if [[ -n "$filename" ]]; then
WORD_LIST_FILE_NAME="$filename"
else
list_files # Allow user to select word file
fi
local start_time=$(date +%s)
local end_time=$((start_time + test_duration))
local correct_words=0
local incorrect_words=0
local keystrokes=0
local words=($(cat "$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/lists/$WORD_LIST_FILE_NAME"))
local word_list=($(generate_word_list 20))
local word_list_top=("${word_list[@]:0:10}")
local word_list_bottom=("${word_list[@]:10}")
local current_word_index=1
local user_input=""
tput civis # Hide cursor
update_state 0
if [[ -t 0 && ! -z "$PS1" ]]; then
stty -echo
fi
# Main loop
while [[ $(date +%s) -lt $end_time ]]; do
remaining_time=$((end_time - $(date +%s)))
read -t $remaining_time -k 1 char || break
keystrokes=$((keystrokes + 1)) # Increment for every keystroke
if [[ "$char" == $'\177' ]]; then # Backspace
user_input=${user_input%?}
update_state
elif [[ "$char" == $'\040' ]]; then # Spacebar
is_correct=1
if [[ "$user_input" == "${word_list[$current_word_index]}" ]]; then
correct_words=$((correct_words + 1))
is_correct=0
else
incorrect_words=$((incorrect_words + 1))
fi
if [[ $current_word_index -ge 10 ]]; then
word_list=("${word_list[@]:10}" $(generate_word_list 10 | cut -d' ' -f1-10))
word_list_top=("${word_list[@]:0:10}")
word_list_bottom=("${word_list[@]:10}")
current_word_index=0
fi
current_word_index=$((current_word_index + 1))
user_input=""
update_state $is_correct
else
user_input+=$char
update_state
fi
done
if [[ -t 0 && ! -z "$PS1" ]]; then
stty echo
fi
# Calculate stats
local elapsed_time=$((end_time - start_time))
local total_words=$((correct_words + incorrect_words))
local wpm=0
if [[ $elapsed_time -gt 0 ]]; then
wpm=$(((correct_words * 60) / elapsed_time))
fi
local accuracy=0
if [[ $total_words -gt 0 ]]; then
accuracy=$((correct_words * 100 / total_words))
fi
local current_date=$(date +"%m/%d/%Y%l:%M%p")
local new_entry="{\"date\":\"$current_date\",\"wpm\":$wpm,\"test duration\":$test_duration,\"keystrokes\":$keystrokes,\"accuracy\":$accuracy,\"correct\":$correct_words,\"incorrect\":$incorrect_words}"
# Update stats logic with jq
local stats=$(_load_history)
if command -v jq &>/dev/null; then
if jq -e . >/dev/null 2>&1 <<<"$stats"; then
# Check if the specified file already exists in the stats data
if jq -e ".\"$WORD_LIST_FILE_NAME\"" >/dev/null 2>&1 <<<"$stats"; then
stats=$(jq --arg file "$WORD_LIST_FILE_NAME" --argjson entry "$new_entry" --argjson new_wpm "$wpm" --argjson new_accuracy "$accuracy" '
.[$file] |= (
.average.["tests taken"] += 1 |
.average.wpm = ((.average.wpm * (.average.["tests taken"] - 1) + $new_wpm) / .average.["tests taken"]) |
.average.accuracy = ((.average.accuracy * (.average.["tests taken"] - 1) + $new_accuracy) / .average.["tests taken"]) |
.results += [$entry]
)
' <<<"$stats")
else
# If the file is new, add it without altering existing data
stats=$(jq --arg file "$WORD_LIST_FILE_NAME" --argjson entry "$new_entry" --argjson new_wpm "$wpm" --argjson new_accuracy "$accuracy" '
. + {($file): {average: {wpm: $new_wpm, accuracy: $new_accuracy, "tests taken": 1}, results: [$entry]}}
' <<<"$stats")
fi
else
# Initialize stats with the first entry if stats file was empty or invalid
stats="{\"$WORD_LIST_FILE_NAME\": {\"average\": {\"wpm\": $wpm, \"accuracy\": $accuracy, \"tests taken\": 1}, \"results\": [$new_entry]}}"
fi
else
# Non-jq fallback (simple string concatenation, if jq isn't available)
if [[ $stats == "{}" ]]; then
stats="{\"$WORD_LIST_FILE_NAME\": {\"average\": {\"wpm\": $wpm, \"accuracy\": $accuracy, \"tests taken\": 1}, \"results\": [$new_entry]}}"
else
stats="${stats/%\}/},\"$WORD_LIST_FILE_NAME\": {\"average\": {\"wpm\": $wpm, \"accuracy\": $accuracy, \"tests taken\": 1}, \"results\": [$new_entry]}}"
fi
fi
# Save stats
local stats_dir="$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/stats"
mkdir -p "$stats_dir"
printf "%s" "$stats" >"$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/stats/stats.json"
local label_width=$(printf '%s\n' "Keystrokes" "Accuracy" "Correct" "Incorrect" | awk '{print length}' | sort -nr | head -n1)
local value_width=10
local label_max_width=$((RESULT_TABLE_WIDTH - value_width - 5))
local result_data=(
"$(printf ' %-*s %*s ' "$label_max_width" "Keystrokes" "$value_width" "$keystrokes")"
"$(printf ' %-*s %*s ' "$label_max_width" "Accuracy" "$value_width" "$accuracy%")"
"$(printf ' %-*s %*s ' "$label_max_width" "Correct" "$value_width" "$correct_words")"
"$(printf ' %-*s %*s ' "$label_max_width" "Incorrect" "$value_width" "$incorrect_words")"
)
_draw_table "$RESULT_TABLE_WIDTH" "Result" "═" "" "$wpm WPM" "" "-" "${result_data[@]}" "-" "" "═" "$WORD_LIST_FILE_NAME"
tput cnorm # Show cursor again
}
# Shows typing speed test history
#
# wpm_history
#
function wpm_history() {
local stats_file="$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/stats/stats.json"
if [[ -f "$stats_file" ]]; then
local stats=$(_load_history)
local value_width=20
local result_table_width=40
local label_max_width=$((result_table_width - value_width - 5))
local stats_array=("History" "═" "")
declare -A stats_lists
for file_name in $(echo "$stats" | jq -r 'keys[]'); do
stats_array+=("-" "$(printf "$file_name")" "-")
# Get the entries for the current file in reverse order
local entries=$(echo "$stats" | jq -c ".\"$file_name\".results | reverse | .[]")
# Process each entry in the results array for the current file
while read -r entry; do
local date=$(echo "$entry" | jq -r '.date')
local wpm=$(echo "$entry" | jq -r '.wpm')
local test_duration=$(echo "$entry" | jq -r '.["test duration"]')
local keystrokes=$(echo "$entry" | jq -r '.keystrokes')
local accuracy=$(echo "$entry" | jq -r '.accuracy')
local correct=$(echo "$entry" | jq -r '.correct')
local incorrect=$(echo "$entry" | jq -r '.incorrect')
stats_array+=(
""
"$date"
"$(printf ' %-*s %*s ' "$label_max_width" "Speed" "$value_width" "$wpm "WPM)"
"$(printf ' %-*s %*s ' "$label_max_width" "Timer" "$value_width" "$test_duration"s)"
"$(printf ' %-*s %*s ' "$label_max_width" "Accuracy" "$value_width" "$accuracy"%)"
"$(printf ' %-*s %*s ' "$label_max_width" "Keystrokes" "$value_width" "$keystrokes")"
"$(printf ' %-*s %*s ' "$label_max_width" "Correct" "$value_width" "$correct")"
"$(printf ' %-*s %*s ' "$label_max_width" "Incorrect" "$value_width" "$incorrect")"
""
)
done <<< "$entries"
# Store the array in stats_lists
stats_lists["$file_name"]="${stats_array[@]}"
done
_draw_table 50 "${stats_array[@]}"
else
echo "No history available."
fi
}
# Shows average results for each word list
#
# wpm_stats
#
function wpm_stats() {
local stats_file="$(dirname "$_OMZ_WPM_PLUGIN_DIR")/wpm/stats/stats.json"
if [[ -f "$stats_file" ]]; then
local stats=$(_load_stats)
local value_width=20
local result_table_width=50
local label_max_width=$((result_table_width - value_width - 5))
local stats_array=("Speed Stats" "═" "")
declare -A stats_lists
for file_name in $(echo "$stats" | jq -r 'keys[]'); do
# Print file name as a header
stats_array+=("-" "$(printf "$file_name")" "-")
# Extract stats for the current file
local average_data=$(echo "$stats" | jq -c ".\"$file_name\"")
local wpm=$(echo "$average_data" | jq -r '.wpm')
local accuracy=$(echo "$average_data" | jq -r '.accuracy')
local tests_taken=$(echo "$average_data" | jq -r '.["tests taken"]')
# Add data to stats_array
stats_array+=(
"$(printf ' %-*s %*s ' "$label_max_width" "Average WPM" "$value_width" "$wpm WPM")"
"$(printf ' %-*s %*s ' "$label_max_width" "Average Accuracy" "$value_width" "$accuracy%")"
"$(printf ' %-*s %*s ' "$label_max_width" "Tests Taken" "$value_width" "$tests_taken")"
""
)
# Store the array in stats_lists
stats_lists["$file_name"]="${stats_array[@]}"
done
_draw_table "$result_table_width" "${stats_array[@]}"
else
echo "No stats available."
fi
}