-
Notifications
You must be signed in to change notification settings - Fork 21
Kai assignment submission #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pranav7002
wants to merge
7
commits into
Git-Lecture-2026:kai
Choose a base branch
from
pranav7002:kai
base: kai
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26db643
Add bandit wargames writeup
pranav7002 21c338a
adding monkey type
pranav7002 640b964
Add improvements to the typing test
pranav7002 7443c9a
Add cli options to choose difficuty and num of words, also result sto…
pranav7002 4dc8af9
Remove accidental string in code
pranav7002 a5a161a
Add git exercise and improve directory structure
pranav7002 e74c5e2
Add --help flag and improve rendering with tput cup 0 0
pranav7002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| #!/bin/bash | ||
|
|
||
| WORD_FILE="/usr/share/dict/words" | ||
| COUNT=20 | ||
| DIFF="medium" | ||
|
|
||
| show_help() { | ||
| echo "flags:" | ||
| echo "-n number of words" | ||
| echo "-d difficulty | easy | medium | hard" | ||
| echo "--help get help" | ||
| } | ||
|
|
||
| if [[ "$1" == "--help" ]]; then | ||
| show_help | ||
| exit 0 | ||
| fi | ||
|
|
||
| # CLI options | ||
| while getopts "n:d:" opt; do | ||
| case $opt in | ||
| n) COUNT=$OPTARG ;; | ||
| d) DIFF=$OPTARG ;; | ||
| esac | ||
| done | ||
|
|
||
| # colors | ||
| GRN="\033[32m" | ||
| RED="\033[31m" | ||
| DIM="\033[2m" | ||
| RST="\033[0m" | ||
| UND="\033[4m" | ||
| UND_OFF="\033[24m" | ||
|
|
||
| # generate words | ||
| generate_words() { | ||
| local count=$1 | ||
| min=$2 | ||
| max=$3 | ||
| grep -E "^[a-z]{$min,$max}$" "$WORD_FILE" | sort -R | head -n "$count" | ||
| } | ||
|
|
||
| # pick words | ||
| case "$DIFF" in | ||
| easy) raw=$(generate_words "$COUNT" 2 5);; | ||
| medium) raw=$(generate_words "$COUNT" 4 8);; | ||
| hard) raw=$(generate_words "$COUNT" 6 12);; | ||
| *) raw=$(generate_words "$COUNT" 3 8);; | ||
| esac | ||
|
|
||
| words=$(echo "$raw" | tr '\n' ' ') | ||
| words="${words% }" | ||
|
|
||
| # draw screen | ||
| render() { | ||
|
|
||
| tput cup 0 0 | ||
| echo "typing test" | ||
| echo "----------------" | ||
| echo | ||
|
|
||
| for ((i=0;i<${#words};i++)); do | ||
| c="${words:$i:1}" | ||
|
|
||
| if ((i < pos)); then | ||
| if [[ "${typed:$i:1}" == "$c" ]]; then | ||
| printf "${GRN}%s${RST}" "$c" | ||
| else | ||
| printf "${RED}%s${RST}" "$c" | ||
| fi | ||
|
|
||
| elif ((i == pos)); then | ||
| printf "${UND}%s${UND_OFF}" "$c" | ||
|
|
||
| else | ||
| printf "${DIM}%s${RST}" "$c" | ||
| fi | ||
| done | ||
|
|
||
| echo | ||
| echo | ||
| } | ||
|
|
||
| # results | ||
| results() { | ||
|
|
||
| secs=$1 | ||
| correct=0 | ||
|
|
||
| for ((i=0;i<${#words} && i<${#typed};i++)); do | ||
| [[ "${typed:$i:1}" == "${words:$i:1}" ]] && ((correct++)) | ||
| done | ||
|
|
||
| wc=$(echo "$words" | wc -w | tr -d ' ') | ||
|
|
||
| wpm=0 | ||
| if ((secs>0)); then | ||
| wpm=$(( (wc * 60) / secs )) | ||
| fi | ||
|
|
||
| acc=0 | ||
| if ((${#words}>0)); then | ||
| acc=$(( (correct * 100) / ${#words} )) | ||
| fi | ||
|
|
||
| clear | ||
| echo | ||
| echo "results" | ||
| echo "-------" | ||
| echo "time: $secs seconds" | ||
| echo "wpm: $wpm" | ||
| echo "accuracy: $acc%" | ||
| echo | ||
|
|
||
| LOG="$HOME/.typetest_log.csv" | ||
|
|
||
| if [[ ! -f "$LOG" ]]; then | ||
| echo "date,time,wpm,accuracy" > "$LOG" | ||
| fi | ||
|
|
||
| echo "$(date +%F),$secs,$wpm,$acc" >> "$LOG" | ||
| } | ||
|
|
||
| # main | ||
| pos=0 | ||
| typed="" | ||
| started=0 | ||
| t0=0 | ||
|
|
||
| echo "press enter to start" | ||
| read | ||
|
|
||
| render | ||
|
|
||
| while ((pos < ${#words})); do | ||
|
|
||
| IFS= read -rsn1 key | ||
|
|
||
| if ((started==0)); then | ||
| t0=$SECONDS | ||
| started=1 | ||
| fi | ||
|
|
||
| # backspace | ||
| if [[ "$key" == $'\x7f' ]]; then | ||
| if ((pos>0)); then | ||
| ((pos--)) | ||
| typed="${typed:0:$pos}" | ||
| fi | ||
| render | ||
| continue | ||
| fi | ||
|
|
||
| [[ -z "$key" ]] && key=" " | ||
|
|
||
| typed+="$key" | ||
| ((pos++)) | ||
|
|
||
| render | ||
|
|
||
| done | ||
|
|
||
| results $((SECONDS - t0)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a
--helpwhich prints the optional args and their possible value.