-
Notifications
You must be signed in to change notification settings - Fork 3
/
dmenu_et
executable file
·142 lines (117 loc) · 3.2 KB
/
dmenu_et
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
ITEMS_LIMIT=15
exit_on_esc () {
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
exit $EXIT_CODE
fi
}
copy_to_clipboard () {
if [[ -z "$XDG_SESSION_TYPE" ]]; then
SESSION_TYPE=$(loginctl show-session "$(loginctl | grep $(whoami) | awk '{ print $1 }')" -p Type --value)
else
SESSION_TYPE="$XDG_SESSION_TYPE"
fi
if [[ "$SESSION_TYPE" == "wayland" ]]; then
echo -n "$1" | wl-copy
else
echo -n "$1" | xclip -r -selection clipboard
fi
}
run_again () {
if [ "$2" ]; then
WORD=$(echo "$2" | dmenu -p "$1" -i -l $ITEMS_LIMIT)
exit_on_esc
WORD_LENGTH=$(echo -n "$WORD" | wc -m)
if [ $WORD_LENGTH -gt 0 ]; then
NEW_WORD=$(echo -n "$WORD" | cut -c 1-$WORD_LENGTH)
echo "$2" | grep -w "$NEW_WORD" > /dev/null
IN_SUGGESTIONS=$?
if [ $IN_SUGGESTIONS -eq 0 ]; then
copy_to_clipboard "$NEW_WORD"
notify-send -a "dmenu_et" "\"$NEW_WORD\" has been copied to clipboard"
exit 0
fi
fi
elif [ "$1" ]; then
WORD=$(printf '%s' | dmenu -p "$1" -i)
exit_on_esc
else
WORD=$(printf '%s' | dmenu -p "$COMMAND" -i)
exit_on_esc
fi
$0 "$WORD"
exit 0
}
get_abbreviations () {
ABBREVIATIONS=$(et -abr "$1")
run_again "What \"$1\" might stand for:" "$ABBREVIATIONS"
}
get_antonyms () {
spell $1 0
ANTONYMS=$(et -ant "$1")
run_again "Antonyms for \"$1\":" "$ANTONYMS"
}
define () {
spell $1 0
DEFINITIONS=$(et -def "$1")
run_again "Definitions for \"$1\":" "$DEFINITIONS"
}
pronounce () {
WORD="$1"
spell "$WORD" 0
SOURCE='macmillan'
SOURCES=$(et -lst "$WORD" | sed 's/^- //')
if [[ $(echo "$SOURCES" | wc -l) -gt 1 ]]; then
SOURCE=$(
echo "$SOURCES" | \
dmenu -p 'Choose pronunciation source/accent: ' -i -l $ITEMS_LIMIT | \
sed 's/ (default)$//'
)
fi
et -pro "$WORD" -src "$SOURCE"
}
spell () {
SUGGESTIONS=$(et -spl "$1")
FOUND=$?
PROCEED_TO_DEFINE=$2
if [ $FOUND -eq 0 ]; then
if [ $PROCEED_TO_DEFINE -ne 0 ]; then
run_again "\"$1\" appears to be spelled correctly"
fi
else
SUGGESTIONS=$(echo "$SUGGESTIONS" | tr -d '\r')
run_again "\"$1\" doesn't appear to be spelled correctly. Suggestions:" "$SUGGESTIONS"
fi
}
get_synonyms () {
spell $1 0
SYNONYMS=$(et -syn "$1")
run_again "Synonyms for \"$1\":" "$SYNONYMS"
}
if [ "$@" ]; then
COMMAND=$(echo "$@" | cut -d " " -f 1)
WORD=$(echo "$@" | cut -d " " -f 2)
SPACE_COUNT=$(echo "$@" | tr -cd " \t" | wc -c)
if [ "$WORD" == "" ] || [ $SPACE_COUNT -lt 1 ]; then
run_again "You need to specifiy a command and a word"
else
if [ "$COMMAND" == "abr" ] || [ "$COMMAND" == "abbreviations" ]; then
get_abbreviations $WORD
elif [ "$COMMAND" == "ant" ] || [ "$COMMAND" == "antonyms" ]; then
get_antonyms $WORD
elif [ "$COMMAND" == "def" ] || [ "$COMMAND" == "define" ]; then
define $WORD
elif [ "$COMMAND" == "pro" ] || [ "$COMMAND" == "pronounce" ]; then
pronounce $WORD
elif [ "$COMMAND" == "spl" ] || [ "$COMMAND" == "spell" ]; then
spell $WORD 1
elif [ "$COMMAND" == "syn" ] || [ "$COMMAND" == "synonyms" ]; then
get_synonyms $WORD
else
run_again "Unkown command \"$COMMAND\""
fi
fi
else
run_again "query:"
fi