2021-06-30 00:36:37 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Quick grep for emojis from the terminal.
|
|
|
|
# You first have to download the UCD archive. It's only a few MB compressed.
|
|
|
|
# I use ripgrep for its speed but you can replace GREP by what you see fit.
|
|
|
|
# Bonus! Use this bash function to copy the first result in your X clipboard:
|
|
|
|
# mj() { emoji -u "$1" | xclip ; echo "$(xclip -o)" }
|
|
|
|
# Made with 💖 by dece. s/o to mon loulou, the bash samurai. License: WTFPLv2.
|
|
|
|
|
|
|
|
UCD_URL="https://www.unicode.org/Public/UCD/latest/ucdxml/ucd.all.flat.zip"
|
2022-08-31 11:41:29 +02:00
|
|
|
DIR="$HOME/.local/share/emoji"
|
|
|
|
LIST="$DIR/emojis.txt.gz"
|
2021-06-30 00:36:37 +02:00
|
|
|
GREP="rg"
|
|
|
|
|
|
|
|
usage() {
|
2021-12-13 15:04:48 +01:00
|
|
|
echo "Usage: $0 [OPTION]... FILTER"
|
2021-06-30 00:36:37 +02:00
|
|
|
echo "Display emojis based on the name filter provided."
|
|
|
|
echo " -h show usage"
|
|
|
|
echo " -n hide emoji name"
|
|
|
|
echo " -l LIMIT limit number of output lines"
|
|
|
|
echo " -u unique result (equals -n and -l 1), no new line"
|
|
|
|
echo " -c show code point"
|
2022-08-31 11:52:08 +02:00
|
|
|
echo " -z use fzf mode"
|
2022-08-31 11:41:29 +02:00
|
|
|
echo " -d download UCD zip to create list file (requires curl)"
|
2021-06-30 00:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[ $# -eq 0 ] && usage && exit
|
|
|
|
|
|
|
|
download_ucdxml() {
|
2022-08-31 11:41:29 +02:00
|
|
|
[ ! -d "$DIR" ] && mkdir -p "$DIR"
|
|
|
|
unc_list="${LIST%.gz}"
|
|
|
|
curl -L "$UCD_URL" | zcat | "$GREP" 'Emoji="Y"' | while read -r line; do
|
|
|
|
codepoint="$(echo "$line" | sed -E 's/.* cp="([0-9A-F]+)".*/\1/g')"
|
|
|
|
name="$(echo "$line" | sed -E 's/.* na="([^"]+)".*/\1/g')"
|
|
|
|
echo "$codepoint;$name" >> "$unc_list"
|
|
|
|
done
|
|
|
|
gzip "$unc_list"
|
2021-06-30 00:36:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HIDE_NAME=
|
|
|
|
LIMIT=
|
|
|
|
NO_NEW_LINE=
|
|
|
|
SHOW_CP=
|
2022-08-31 11:52:08 +02:00
|
|
|
FZF_MODE=
|
|
|
|
while getopts "hdnl:ucz" OPTION; do
|
2021-06-30 00:36:37 +02:00
|
|
|
case $OPTION in
|
|
|
|
h) usage; exit 0 ;;
|
|
|
|
d) download_ucdxml; exit $? ;;
|
|
|
|
n) HIDE_NAME=true ;;
|
|
|
|
l) LIMIT=$OPTARG ;;
|
|
|
|
u) HIDE_NAME=true; LIMIT=1; NO_NEW_LINE=true ;;
|
|
|
|
c) SHOW_CP=true ;;
|
2022-08-31 11:52:08 +02:00
|
|
|
z) FZF_MODE=true ;;
|
2021-06-30 00:36:37 +02:00
|
|
|
*) usage; exit 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( OPTIND - 1 ))
|
|
|
|
FILTER="$*"
|
|
|
|
|
2022-08-31 11:41:29 +02:00
|
|
|
if [ ! -f "$LIST" ]; then
|
|
|
|
echo "Can't find list file at $LIST. Use -d to download it!"
|
2021-06-30 00:36:37 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-08-31 11:52:08 +02:00
|
|
|
find_emojis() {
|
|
|
|
line_id=0
|
|
|
|
zcat "$LIST" | "$GREP" -i "$FILTER" | while read -r line; do
|
|
|
|
[ -n "$LIMIT" ] && (( line_id >= LIMIT )) && break
|
|
|
|
readarray -d ";" -t elements <<< "$line"
|
|
|
|
codepoint="${elements[0]}"
|
|
|
|
result="$(echo -e "\\U$codepoint")"
|
|
|
|
if [ "$HIDE_NAME" != true ]; then
|
|
|
|
name="${elements[1]}"
|
|
|
|
result="$result $(echo "$name" | tr '[:upper:]' '[:lower:]')"
|
|
|
|
fi
|
|
|
|
if [ "$SHOW_CP" = true ]; then
|
|
|
|
result="$result (U+$codepoint)"
|
|
|
|
fi
|
|
|
|
[ "$NO_NEW_LINE" = true ] && echo_opt="-n" || echo_opt=""
|
|
|
|
echo "$echo_opt" "$result"
|
|
|
|
line_id=$(( line_id + 1 ))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$FZF_MODE" = true ]; then
|
|
|
|
result="$(find_emojis | fzf)"
|
|
|
|
echo "$result"
|
|
|
|
if command -v xclip > /dev/null; then
|
|
|
|
echo -n "$(awk '{print $1}' <<< "$result")" | xclip
|
|
|
|
echo "(copied to X clipboard)"
|
2021-06-30 00:36:37 +02:00
|
|
|
fi
|
2022-08-31 11:52:08 +02:00
|
|
|
else
|
|
|
|
find_emojis
|
|
|
|
fi
|