summaryrefslogblamecommitdiff
path: root/dict.sh
blob: 137011ae06fe5f370d4b7facb1efd3d3ec3a1d00 (plain) (tree)
1
2
3
4
5
         
 


                                                            













                                            
                                        




                   
                                                                                   
























                                                                  
#!/bin/sh

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

## Configuration
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"