about summary refs log tree commit diff
path: root/nixos/scripts/dm-dictionary
blob: e9b60e2ee9366609ea51fea267ff257998178703 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/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 "$@"