diff options
Diffstat (limited to 'games/mtg/deckLister.sh')
-rwxr-xr-x | games/mtg/deckLister.sh | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/games/mtg/deckLister.sh b/games/mtg/deckLister.sh new file mode 100755 index 0000000..606ca73 --- /dev/null +++ b/games/mtg/deckLister.sh @@ -0,0 +1,62 @@ +#!/bin/sh + +# deckLister.sh: Interactively reads card names in any language, searches for +# them and writes them in English line by line. +# +# Usage: deckLister.sh [-f FILE] + +scriptName="${0##*/}" + +usageMessage="Usage: ${scriptName} [-f FILE]" + +error() { + printf '%s error: %s\n' "$scriptName" "$*" >&2 +} + +errorAndUsage() { + printf '%s error: %s\n%s\n' "$scriptName" "$*" "$usageMessage" >&2 +} + +file='' +welcomeMessage="${scriptName}: Input non-ambiguous card names in any language." +prompt='> ' + +# Process options +while getopts ':f:' opt; do + case $opt in + + 'f' ) + file="$OPTARG" + ;; + + '?' ) + printf '%s\n' "$usageMessage" >&2 + exit 1 + + esac +done +shift $((OPTIND - 1)) + +[ -n "$1" ] && errorAndUsage 'Too many arguments.' && exit 1 + +log() { + if [ -n "$file" ]; then + printf '%s\n' "$@" >> "$file" + else + printf '%s\n' "$@" + fi +} + +nameToEnglish() { + json="$(getScryfallJSON.sh "$@")" || return 1 + echo "$json" | jq -r .name +} + +echo "$welcomeMessage" >&2 + +while true; do + printf '%s' "$prompt" >&2 + read -r cardname + englishName="$(nameToEnglish "$cardname")" + [ -n "$englishName" ] && log "$englishName" +done |