summaryrefslogtreecommitdiff
path: root/dict.sh
diff options
context:
space:
mode:
Diffstat (limited to 'dict.sh')
-rwxr-xr-xdict.sh51
1 files changed, 51 insertions, 0 deletions
diff --git a/dict.sh b/dict.sh
new file mode 100755
index 0000000..53c10df
--- /dev/null
+++ b/dict.sh
@@ -0,0 +1,51 @@
+#!/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"