#!/usr/bin/env bash # # Script name: dm-dictionary # Description: Uses the translate package as a dictionary. # Dependencies: dmenu, fzf, rofi, translate-shell, didyoumean # GitLab: https://www.gitlab.com/dwt1/dmscripts # License: https://www.gitlab.com/dwt1/dmscripts/LICENSE # Contributors: Francesco Prem Solidoro # Derek Taylor # Set with the flags "-e", "-u","-o pipefail" cause the script to fail # if certain things happen, which is a good thing. Otherwise, we can # get hidden bugs that are hard to discover. set -euo pipefail # shellcheck disable=SC1091 source ./_dm-helper.sh 2>/dev/null || source _dm-helper.sh 2>/dev/null source_dmscripts_configs if configs_are_different; then echo "$(date): configs are different" >>"$DM_CONFIG_DIFF_LOGFILE" sleep 1 fi main() { # dmenu expects some sort of input piped into it. # The echo to /dev/null is just a hacky way of giving # dmenu some input without really giving any input. # shellcheck disable=SC2260 word="$(echo "" >/dev/null | ${MENU} "Enter word to lookup:")" testword="$(dym -c -n=1 "$word")" if [ "$word" != "$testword" ]; then keyword=$(dym -c "$word" | ${MENU} "was $word a misspelling?(select/no)") if [ "$keyword" = "no" ] || [ "$keyword" = "n" ]; then keyword="$word" fi else keyword="$word" fi if ! [ "${keyword}" ]; then printf 'No word inserted\n' >&2 exit 0 fi $DMTERM trans -v -d "$keyword" } mainfzf() { # shellcheck disable=SC2260 word="$(echo " " | fzf --bind 'return:print-query' --prompt "Enter word to lookup:")" testword="$(dym -c -n=1 "$word")" if [ "$word" != "$testword" ]; then # shellcheck disable=SC2153 keyword=$(dym -c "$word" | ${FMENU} "was $word a misspelling?(select/no)") if [ "$keyword" = "no" ] || [ "$keyword" = "n" ]; then keyword="$word" fi else keyword="$word" fi if ! [ "${keyword}" ]; then printf 'No word inserted\n' >&2 exit 0 fi $DMTERM trans -v -d "$keyword" } no_opt=1 # If script is run with '-d', it will use 'dmenu' # If script is run with '-f', it will use 'fzf' # If script is run with '-d', it will use 'rofi' while getopts "dfrh" arg 2>/dev/null; do case "${arg}" in d) # shellcheck disable=SC2153 MENU=${DMENU} [[ "${BASH_SOURCE[0]}" == "${0}" ]] && main ;; f) # shellcheck disable=SC2153 [[ "${BASH_SOURCE[0]}" == "${0}" ]] && mainfzf ;; r) # shellcheck disable=SC2153 MENU=${RMENU} [[ "${BASH_SOURCE[0]}" == "${0}" ]] && main "@" ;; h) help ;; *) printf '%s\n' "Error: invalid option" "Type $(basename "$0") -h for help" ;; esac no_opt=0 done # If script is run with NO argument, it will use 'dmenu' [ $no_opt = 1 ] && MENU=${DMENU} && [[ "${BASH_SOURCE[0]}" == "${0}" ]] && main "$@"