~skiqqy/bin

ref: 11eeb22bb6b46b63a9752e13ad708317bdc515cc bin/dict -rwxr-xr-x 827 bytes
11eeb22bStephen Cochrane Added helper script a month ago
                                                                                
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
#!/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 "$@"