summaryrefslogblamecommitdiff
path: root/dict.sh
blob: 53c10df57a71642a75cd4fb89c49032d751868ab (plain) (tree)


















































                                                                                    
#!/bin/sh
## dict.sh - nicely look up words in dictionaries with dictd

## Configuration
dmenu="rofi -dmenu"
pager="rofi -location 2 -width -81 -e"


## Error codes
NO_TERM_PROVIDED=4
NO_DICT_PROVIDED=5


## Get available dictionaries
get_dict() {
	dict -D | \
		tail -n +2 | \
		grep -v "fd" | \
		sed 's/^ [a-z0-9-]* *//' | \
		$dmenu -i -p "Dictionary"
}


## Get search term
get_search_term() {
	term=$(printf "Clipboard\nPrimary selection\n" | $dmenu -i -p "Search term")
	if [ "$term" = "Clipboard" ]; then
		echo "$(xclip -o -selection clipboard)"
	elif [ "$term" = "Primary selection" ]; then
		echo "$(xclip -o -selection primary)"
	else
		echo "$term"
	fi
}


## Search dictionary for term
search() {
	dict=$1
	term=$2
	dict -d $(dict -D | grep "$dict" | cut -w -f2) "$term"
}


## Main
dict=$(get_dict)
[ -z "$dict" ] && exit $NO_DICT_PROVIDED
term=$(get_search_term)
[ -z "$term" ] && exit $NO_TERM_PROVIDED
result=$(search "$dict" "$term")
[ -z "$result" ] && $pager "No results found." || $pager "$result"