-
Notifications
You must be signed in to change notification settings - Fork 4
/
limoji
executable file
·245 lines (210 loc) · 7.97 KB
/
limoji
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
# Limoji 1.0.1
limoji_version="1.0.1"
readonly limoji_version
# Functions for printing colored symbols to the console
printCross() {
printf -- '[\e[1;31m✗\e[0m] \e[1;31m%b\e[0m\n' "${*}"
}
printInfo() {
printf -- '[\e[1;93mi\e[0m] %b\n' "${*}"
}
printQuestion() {
printf -- '\e[0m[\e[1;94m?\e[0m] %b' "${*}"
}
printTick() {
printf -- '[\e[1;32m✓\e[0m] \e[1;32m%b\e[0m\n' "${*}"
}
# A function that prints the selected emoticon to the console
printEmoticon() {
printTick "${1} was copied to the clipboard successfully:"
printf -- '%b' "${!1}" | tee >(copyToClipboard)
printf -- '%s\n' ""
}
# Print a list of all the available commands
printCmdHelp() {
cat << EOF
Usage: limoji <emoticon>
-e, --emoticons Show a list of the available emoticons
-h, --help Show this list
-i, --info Show info about the project
-r, --random Print a random emoticon
-v, --version Display Limoji's version
For more information you can visit our GitHub repository:
https://github.com/GEROGIANNIS/Limoji
EOF
}
# Print info about the project
printCmdInfo() {
printInfo \
"Limoji is an open source tool that makes it easy to choose between
hundreds of cool Unicode emoticons and share them with your friends,
all done using the terminal!
All you have to do is pick the right one or let Limoji choose it for you!
"
}
# Print Limoji's version
printCmdVersion() {
# Print current version
printf -- 'Limoji %b' "${limoji_version}"
# Don't check for new versions if curl is not installed or an argument has been passed
if [[ $(command -v curl) && ! "$1" =~ (--no-update-check|-nu) ]]; then
# Query GitHub's API to see if a new version is available
latest_version=$(fetchLatestRelease "gerogiannis/limoji")
# Compare current version against the one fetched from the API
if [[ "$limoji_version" < "$latest_version" ]]; then
printf -- ' (\e[1;93mUpdate available\e[0m)\n\n'
printInfo "Limoji $latest_version is available."
# Check if Limoji's parent directory is a git repository
if [[ $is_git ]]; then
printf -- ' %s\n' "Run 'git pull' to update."
fi
else
printf -- ' (\e[1;32mUp to date\e[0m)\n'
fi
else
printf -- '\n'
fi
}
# Fetch latest release from GitHub
# Based on lukechilds/get_latest_release.sh
# v0.31.4
fetchLatestRelease() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub API
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
# Function that picks a random emoticon name
fetchRandomName() {
# Generate a random number between 1 and unicode line count
randomLine=$(( 2 + RANDOM % (UNICODE_LINES - 1) ))
# Fetch emoticon's name from the selected line
randomName=$(sed "${randomLine}q;d" "${config_path}"/unicode | cut -d= -f1)
}
# Function that picks a random emoticon
randomEmoticon() {
local answer
answer="y"
# Until the user replies with something other than 'y', cycle through random emoticons
# If the user replies with 'Y', it will be lowercased (i.e. ${var,,}) for the test
until [[ "${answer,,}" != y ]]; do
# Picks a random emoticon name
fetchRandomName
# Print the emoticon and copy it to the clipboard
printEmoticon "${randomName}"
# Ask the user if he wants another emoticon
read -n 1 -r -p "$(printQuestion "Do you want another one? (y/n) ")" answer
printf -- '%s\n' ""
done
}
# Function that creates a list of the available emoticons
listEmoticons() {
# Repeat for every single line
for (( c=2; c<=UNICODE_LINES; c++ )); do
# Read emoticon's name
name=$(sed "${c}q;d" "${config_path}"/unicode | cut -d= -f1)
# Print the name followed by the emoticon itself
printf -- '%b:\n%b\n\n' "${name}" "${!name}"
done
}
# Prints an error message on invalid input
invalidArgument() {
printCross "Invalid argument!"
printf -- '%s\n' "Try 'limoji --help' for a list of available commands."
}
# Attempt to figure out a clipboard handling method
setupClipboard() {
case "${XDG_SESSION_TYPE}" in
(x11)
# If X11 and xclip are simultaneously present, that's a preferred option
if command -v xclip >/dev/null 2>&1; then
copyToClipboard() { xclip -selection c; }
return 0
else
printInfo 'X11 detected'
printCross 'xclip is required for copying text to the clipboard'
exit 1
fi
;;
(wayland)
# If Wayland and wl-copy are simultaneously present, that's a preferred option
if command -v wl-copy >/dev/null 2>&1; then
copyToClipboard() { wl-copy; }
return 0
# Fallback if wl-copy isn't installed
elif command -v xclip >/dev/null 2>&1; then
copyToClipboard() { xclip -selection c; }
return 0
else
printInfo 'Wayland detected'
printCross 'wl-clipboard is required for copying text to the clipboard'
exit 1
fi
;;
(*)
# If we get to this point, then it only means that XDG_SESSION_TYPE isn't set
# We can run some more thorough heuristics to try and find a way to clipboard things
if command -v wl-copy >/dev/null 2>&1; then
copyToClipboard() { wl-copy; }
return 0
elif command -v xclip >/dev/null 2>&1; then
copyToClipboard() { xclip -selection c; }
return 0
elif command -v xsel >/dev/null 2>&1; then
copyToClipboard() { xsel --clipboard --input; }
return 0
elif command -v pbcopy >/dev/null 2>&1; then
copyToClipboard() { pbcopy; }
return 0
else
printCross "Your system is not supported yet, please create a new issue here:"
printf -- '%s\n' "https://github.com/GEROGIANNIS/Limoji/issues/new/choose" >&2
exit 1
fi
;;
esac
}
main() {
# Fail fast checks.
# First, if no args are given, give the invalid argument message and exit
(( ${#} == 0 )) && { invalidArgument; exit 1; }
# Next, try to get a clipboard working
setupClipboard
# Fetch all emoticons from the file
source "${config_path}"/unicode
# Store the number of emoticons in a variable
UNICODE_LINES=$(wc -l < "${config_path}"/unicode)
case "${1}" in
(--emoticons|-e) listEmoticons ;;
(--help|-h) printCmdHelp ;;
(--info|-i) printCmdInfo ;;
(--random|-r) randomEmoticon ;;
(--version|-v) printCmdVersion "$2" ;;
(*[!0-9a-zA-Z]*) invalidArgument ;;
(*)
# Convert all uppercase characters to lowercase
set -- "${1,,}"
# Check if the selected emoticon exists
if [[ -n "${!1}" ]]; then
printEmoticon "${1}"
else
printCross "The specified emoji doesn't exist!"
printf -- '%s\n' "Try 'limoji --emoticons' for a list of available emojis."
fi
;;
esac
}
# Find Limoji's parent path
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P )
# Check if Limoji is installed and config files are located in /etc/limoji
if [[ ! -d $parent_path/.git && -d /etc/limoji ]]; then
config_path="/etc/limoji"
# Otherwise, look for the config files in the parent directory
# A user might have deleted the .git folder, so we don't need to explicitly check for it
else
# Set this flag for later use
is_git=true
# The config files are located in the parent directory
config_path=$parent_path
fi
main "${@}"