Skip to content

Instantly share code, notes, and snippets.

@netj
Created May 25, 2010 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netj/412840 to your computer and use it in GitHub Desktop.
Save netj/412840 to your computer and use it in GitHub Desktop.
an effective tool for instilling vocabularies into your brain
#!/bin/bash
# The "Blinker" (or "깜빡이" in Korean) for Mac
# an effective tool for instilling vocabularies into your brain
#
# Usage: blinker [-OPTION...] SET...
#
# SET is the path to a file where each of its line contains a word and its
# definition separated by tab character. You can get lots of quality sets
# from http://quizlet.com/.
#
# Following OPTIONs are available:
# -l LEARNCOUNT Number of words to learn in straight (Default:10)
# -g GUESSCOUNT Number of words to guess in straight (Default:3)
# -d MINPAUSE Minimum delay (in seconds) to have between words
# -D MAXPAUSE Maximum delay (in seconds) to have between words
# -h Show this message
#
#
# Author: Jaeho Shin <netj@sparcs.org>
# Created: 2010-05-25
set -e
usage() { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }
# check prerequisites
type open grep sed osascript growlnotify shuf >/dev/null
# stuffs for Blinker
BLINKERHOME="$HOME/.blinker"
mkdir -p "$BLINKERHOME"
BlinkerSession=`mktemp -d "$BLINKERHOME/session.XXXXXX"`
trap "rm -rf $BlinkerSession" EXIT QUIT INT TERM
WordBuffer="$BlinkerSession/buffer"
WordHistory="$BLINKERHOME/history"
# default behavior
LengthOfLearningSessions=3
LengthOfGuessingSessions=1
MinDelayBetweenWords=3
MaxDelayBetweenWords=10
# process arguments
while getopts "hl:g:D:d:" o; do
case $o in
h) usage ;;
l) LengthOfLearningSessions=$OPTARG ;;
g) LengthOfGuessingSessions=$OPTARG ;;
d) MinDelayBetweenWords=$OPTARG ;;
D) MaxDelayBetweenWords=$OPTARG ;;
*) echo "$o: unknown option" >&2 ;;
esac
done
shift $(($OPTIND - 1))
[ $# -gt 0 ] || usage
Files=("$@")
# our primitive vocabularies
check-guess() {
local word=$1
local definition=$2
osascript -e '
tell application "Dictionary"
activate
set result to display dialog "'"$word"'\n'"$definition"'\n\nIs it what you thought?" buttons {"Yes", "No", "Lookup"} default button 1 with title "Check Your Guess" with icon note
return the button returned of result
end tell
'
}
pick-words() {
local count=$1
if ! [ -e "$WordBuffer" ]; then
if [ -e "$WordHistory" ]; then
grep -viF -f "$WordHistory" "${Files[@]}"
else
cat "${Files[@]}"
fi >"$WordBuffer"
fi
# TODO reset the history if none remains
# shuffle excluding ones already learned
shuf -n $count "$WordBuffer"
}
add-to-history() {
# add to history and
for word; do
echo "$word"
done >>"$WordHistory"
# remove from WordBuffer
grep -viF "$word" "$WordBuffer" >"$WordBuffer~"
mv -f "$WordBuffer~" "$WordBuffer"
}
blink() {
growlnotify --name "Blinker" --appIcon Dictionary --wait "$@"
}
maxVariableDelay=$(($MaxDelayBetweenWords
- $MinDelayBetweenWords))
pause() {
sleep $(($MinDelayBetweenWords
+ $maxVariableDelay * $RANDOM/32768))
}
# our modes of learning
mode=learn
show-the-word-with-definition() {
if blink "$word" <<<"$definition"; then
sleep 0.5 || break
else
pause || break
fi
}
learn() {
mode=false
pick-words $LengthOfLearningSessions |
while IFS=" " read -r word definition; do
show-the-word-with-definition
done &&
mode=guess
}
guess() {
mode=false
pick-words $LengthOfGuessingSessions |
while IFS=" " read -r word definition; do
if blink "$word?" --message "Guess and click if you know"; then
case "`check-guess "$word" "$definition"`" in
Yes) add-to-history "$word" ;;
No) echo "$word" ;;
Lookup) echo "$word"
# TODO look up in the Dictionary.app itself
open "http://www.google.com/search?q=define:$word"
;;
esac
else
show-the-word-with-definition
fi
done &&
mode=learn
}
# now, start blinking
while :; do $mode; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment