2018-02-24 11:08:40 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Use rofi to pick emoji because that's what this
|
|
|
|
# century is about apparently...
|
|
|
|
#
|
|
|
|
# Requirements:
|
2018-04-12 02:20:08 +00:00
|
|
|
# rofi, xclip, xdotool, curl, xmllint
|
2018-02-24 11:08:40 +00:00
|
|
|
#
|
|
|
|
# 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".
|
|
|
|
# * Confirming an item will automatically paste it WITHOUT
|
|
|
|
# writing it to your clipboard.
|
|
|
|
# * Ctrl+C will copy it to your clipboard WITHOUT pasting it.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Where to save the emojis file.
|
2018-04-12 02:20:08 +00:00
|
|
|
EMOJI_FILE="${XDG_CACHE_DIR:-$HOME/.cache}/emojis.txt"
|
2018-02-24 11:08:40 +00:00
|
|
|
|
|
|
|
# Urls of emoji to download.
|
|
|
|
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/'
|
2018-03-14 02:16:24 +00:00
|
|
|
'https://emojipedia.org/modifiers/'
|
2018-02-24 11:08:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
function download() {
|
2018-04-12 02:20:08 +00:00
|
|
|
notify "$(basename "$0")" 'Downloading all emoji for your pleasure'
|
|
|
|
|
|
|
|
echo -n > "$EMOJI_FILE"
|
2018-02-24 11:08:40 +00:00
|
|
|
|
|
|
|
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 \2/p')
|
|
|
|
|
|
|
|
echo "$emojis" >> "$EMOJI_FILE"
|
|
|
|
done
|
2018-04-12 02:20:08 +00:00
|
|
|
|
|
|
|
notify "$(basename "$0")" "We're all set!"
|
|
|
|
}
|
|
|
|
|
|
|
|
function toclip() {
|
|
|
|
xclip -i -selection primary
|
|
|
|
xclip -o -selection primary | xclip -i -selection clipboard
|
2018-02-24 11:08:40 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 02:20:08 +00:00
|
|
|
function repeat() {
|
|
|
|
local rplc str="$1" count="$2"
|
|
|
|
rplc="$(printf "%${count}s")"
|
|
|
|
echo "${rplc// /"$str"}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function notify() {
|
|
|
|
if which notify-send 1>/dev/null 2>/dev/null; then
|
|
|
|
notify-send "$1" "$2"
|
|
|
|
fi
|
|
|
|
}
|
2018-02-24 11:08:40 +00:00
|
|
|
|
|
|
|
function display() {
|
|
|
|
emoji=$(cat "$EMOJI_FILE" | grep -v '#' | grep -v '^[[:space:]]*$')
|
2018-04-12 02:20:08 +00:00
|
|
|
update="⏫ Update emoji cache"
|
|
|
|
emoji=$(printf "$emoji\n$update")
|
|
|
|
line=$(echo "$emoji" | rofi -dmenu -i -p emoji -kb-custom-1 Ctrl+c -kb-row-tab '' -kb-row-select Tab $@)
|
2018-02-24 11:08:40 +00:00
|
|
|
exit_code=$?
|
|
|
|
|
2018-04-12 02:20:08 +00:00
|
|
|
if [ "${line[@]}" == "$update" ]; then
|
|
|
|
download
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-02-24 11:08:40 +00:00
|
|
|
line=($line)
|
2018-04-12 02:20:08 +00:00
|
|
|
last=${line[${#line[@]}-1]}
|
|
|
|
|
|
|
|
quantifier="${last:${#last}-1:1}"
|
|
|
|
echo "$quantifier" | egrep -q '^[0-9]+$' || quantifier=1
|
|
|
|
emoijs="$(repeat "${line[0]}" "$quantifier")"
|
2018-02-24 11:08:40 +00:00
|
|
|
|
|
|
|
if [ $exit_code == 0 ]; then
|
2018-04-12 02:20:08 +00:00
|
|
|
xdotool type --clearmodifiers "$emoijs"
|
|
|
|
echo -n "$emoijs" | xclip -i
|
2018-02-24 11:08:40 +00:00
|
|
|
elif [ $exit_code == 10 ]; then
|
2018-04-12 02:20:08 +00:00
|
|
|
echo -n "$emoijs" | xclip -i
|
2018-02-24 11:08:40 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if [[ "$1" =~ -D|--download ]]; then
|
|
|
|
download
|
|
|
|
exit 0
|
|
|
|
elif [[ "$1" =~ -h|--help ]]; then
|
|
|
|
echo "usage: $0 [-D|--download]"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
display
|