-
Notifications
You must be signed in to change notification settings - Fork 3
/
rofi_et
executable file
·194 lines (159 loc) · 4.82 KB
/
rofi_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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" | rofi -dmenu -p $COMMAND -i -mesg "$1" -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 "rofi_et" "\"$NEW_WORD\" has been copied to clipboard"
exit 0
fi
fi
elif [ "$1" ]; then
WORD=$(rofi -dmenu -p $COMMAND -i -mesg "$1" -no-fixed-num-lines -l 0)
exit_on_esc
else
WORD=$(rofi -dmenu -p $COMMAND -i)
exit_on_esc
fi
$0 "$WORD"
exit 0
}
get_abbreviations () {
ABBREVIATIONS=$(et -abr "$1")
run_again "<b><i>What \"$1\" might stand for:</i></b>" "$ABBREVIATIONS"
}
get_antonyms () {
COMMAND="spell"
spell $1 0
COMMAND="ant"
ANTONYMS=$(et -ant "$1")
run_again "<b><i>Antonyms for \"$1\":</i></b>" "$ANTONYMS"
}
define () {
COMMAND="spell"
spell $1 0
COMMAND="define"
DEFINITIONS=$(et -def "$1")
run_again "<b><i>Definitions for \"$1\":</i></b>" "$DEFINITIONS"
}
pronounce () {
WORD="$1"
COMMAND="spell"
spell "$WORD" 0
COMMAND="pronounce"
SOURCE='macmillan'
SOURCES=$(et -lst "$WORD" | sed 's/^- //')
if [[ $(echo "$SOURCES" | wc -l) -gt 1 ]]; then
SOURCE=$(
echo "$SOURCES" | \
rofi -dmenu -p "$COMMAND" -i -mesg "<b><i>Choose pronunciation source/accent: </i></b>" -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 "<span foreground=\"green\">\"$1\" appears to be spelled correctly</span>"
fi
else
MESG=$(
cat << EOF
<span foreground="red">"$1" doesn't appear to be spelled correctly.</span>
<b><i>Suggestions:</i></b>
EOF
)
run_again "$MESG" "$SUGGESTIONS"
fi
}
get_synonyms () {
COMMAND="spell"
spell $1 0
COMMAND="syn"
SYNONYMS=$(et -syn "$1")
run_again "<b><i>Synonyms for \"$1\":</i></b>" "$SYNONYMS"
}
usage () {
cat << EOF
<u>Usage (in rofi's or dmenu's input bar)</u>:
COMMAND [WORD]
<u>Commands</u>:
<b><i>abr</i></b>, <b><i>abbreviations <word></i></b>
Get what <word> might stand for from abbreviations.com.
<b><i>ant</i></b>, <b><i>antonyms <word></i></b>
Get antonyms for <word> from bighugelabs.com.
<b><i>def</i></b>, <b><i>define <word></i></b>
Get definitions for <word> from wordnik.com if correctly spelled, otherwise display spell suggestions.
<b><i>pro</i></b>, <b><i>pronounce <word></i></b>
Play pronunciation of <word> from wordnik.com and dictionaryapi.dev.
<b><i>spl</i></b>, <b><i>spell <word></i></b>
Get spell suggestions for <word> from wordlist if not spelled correctly, otherwise display a message indicating that <word> is spelled correctly.
<b><i>syn</i></b>, <b><i>synonyms <word></i></b>
Get synonyms for <word> from bighugelabs.com.
<b><i>help</i></b>
Display this help message
EOF
}
COMMAND="query"
if [ "$@" ]; then
COMMAND=$(echo "$@" | cut -d " " -f 1)
WORD=$(echo "$@" | cut -d " " -f 2)
if [ "$COMMAND" == "help" ]; then
run_again "$(usage)"
else
SPACE_COUNT=$(echo "$@" | tr -cd " \t" | wc -c)
if [ "$WORD" == "" ] || [ $SPACE_COUNT -lt 1 ]; then
COMMAND="query"
run_again "<span foreground=\"red\">You need to specifiy a command and a word</span>"
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
UNKNOWN_COMMAND=$COMMAND
COMMAND="query"
run_again "<span foreground=\"red\">Unkown command \"$UNKNOWN_COMMAND\"</span>"
fi
fi
fi
else
run_again
fi