~skiqqy/bin

11eeb22bb6b46b63a9752e13ad708317bdc515cc — Stephen Cochrane 9 days ago e0a9799 master
Added helper script
1 files changed, 54 insertions(+), 0 deletions(-)

A dict
A dict => dict +54 -0
@@ 0,0 1,54 @@
#!/usr/bin/env bash
# Get descriptions for words, basically a shitty curl+jq wrapper
# Please go support https://dictionaryapi.dev/

usage() {
	cat << EOF
$ dict [options] WORDS...

Written by skiqqy
EOF
	exit "${1:-0}"
}

# Fetch description(s) for a word
# Usage: fetch WORD
fetch()
{
	word=${1:-word} # Todo, maybe add a word of the day if no arg given

	printf '%s\n' "$word"
	while read -r definition
	do
		printf "\n - %s\n" "${definition//\"/}"
	done < <(curl -s "$api$word" | jq '.[].meanings[].definitions[].definition')
	printf '\n'
}

main()
{
	api='https://api.dictionaryapi.dev/api/v2/entries/en/'

	while getopts h opt
	do
		case "$opt" in
			h)
				usage
				;;
			*)
				usage 1
				;;
		esac
	done
	shift "$((OPTIND-1))"

	[ -z "$1" ] && usage

	while [ -n "$1" ]
	do
		fetch "$1"
		shift 1
	done
}

main "$@"