-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrofi-emoji
executable file
·134 lines (106 loc) · 3.28 KB
/
rofi-emoji
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
#!/usr/bin/env bash
#
# Use rofi to pick emoji because that's what this
# century is about apparently...
#
# Requirements:
# rofi, xsel, xdotool, curl, libxml2
#
# Usage:
# 1. Download all emoji
# $ rofi-emoji --download
#
# 2. Run it!
# $ rofi-emoji
#
# Notes:
# * You'll need a emoji font like "Noto Emoji" or "EmojiOne".
# * "Return" will automatically paste it WITHOUT writing it to your
# clipboard.
# * "Ctrl+C" will copy it to your clipboard WITHOUT pasting.
#
# Basic rofi launch command. Additional option can also be provided
# when calling this script.
LAUNCHER="rofi -dmenu -i -p emoji -kb-custom-1 Ctrl+c $@"
# Where to save the emojis to.
EMOJI_FILE="$HOME/.cache/emojis.txt"
EMOJI_HIST_FILE="$HOME/.cache/emojis.history"
DELIMITER=' '
# Urls of emoji to download.
# You can remove what you don't need.
URLS=(
'https://emojipedia.org/people/'
'https://emojipedia.org/nature/'
'https://emojipedia.org/food-drink/'
'https://emojipedia.org/activity/'
'https://emojipedia.org/travel-places/'
'https://emojipedia.org/objects/'
'https://emojipedia.org/symbols/'
'https://emojipedia.org/flags/'
)
function notify() {
if [ "$(command -v notify-send)" ]; then
notify-send "$1" "$2"
fi
}
function download() {
notify `basename "$0"` 'Downloading all emoji for your pleasure'
echo "" > "$EMOJI_FILE"
for url in "${URLS[@]}"; do
echo "Downloading: $url"
# Download the list of emoji and remove all the junk around it
emojis=$(curl -s "$url" | \
xmllint --html \
--xpath '//ul[@class="emoji-list"]' - 2>/dev/null)
# Get rid of starting/closing ul tags
emojis=$(echo "$emojis" | head -n -1 | tail -n +1)
# Extract the emoji and its description
emojis=$(echo "$emojis" | \
sed -rn "s/.*<span class=\"emoji\">(.*)<\/span> (.*)<\/a><\/li>/\\1$DELIMITER\\2/p")
echo "$emojis" >> "$EMOJI_FILE"
done
notify `basename "$0"` "We're all set!"
}
function display() {
touch "$EMOJI_HIST_FILE"
emoji_hist=$(cat "$EMOJI_HIST_FILE")
emoji_orig=$(cat "$EMOJI_FILE")
# Combined emojis without:
# - commented lines
# - empty lines
# - duplicates (due to history)
emoji=$(echo -e "$emoji_hist\n$emoji_orig" \
| grep -v '#' | grep -v '^[[:space:]]*$' | awk '!x[$0]++')
# Display without the history count
line=$(echo "$emoji" | $LAUNCHER)
exit_code=$?
if [ $exit_code -eq 1 ]; then
exit 0
fi
# Add to top of the history
emoji_hist="${line}\n${emoji_hist}"
# Rewrite history file without duplicates
echo -e "$emoji_hist" | awk '!x[$0]++' > "$EMOJI_HIST_FILE"
# Extract emoji
moji=$(echo "$line" | awk -F " " '{print $1}')
if [ $exit_code -eq 0 ]; then
echo -n $moji | xsel -i -p -b
xdotool key Ctrl+Shift+v
elif [ $exit_code -eq 10 ]; then
echo -n $moji | xsel -i -p -b
fi
}
# Some simple argparsing
if [[ "$1" =~ -D|--download ]]; then
download
exit 0
elif [[ "$1" =~ -h|--help ]]; then
echo "usage: $0 [-D|--download]"
exit 0
fi
# Download all emoji if they don't exist yet
if [ ! -f "$EMOJI_FILE" ]; then
download
fi
# display displays :)
display