Compare commits
2 commits
392727e169
...
cc6b56e7a7
Author | SHA1 | Date | |
---|---|---|---|
cc6b56e7a7 | |||
b34e1121c1 |
49
emoji.sh
49
emoji.sh
|
@ -7,7 +7,8 @@
|
||||||
# Made with 💖 by dece. s/o to mon loulou, the bash samurai. License: WTFPLv2.
|
# 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"
|
UCD_URL="https://www.unicode.org/Public/UCD/latest/ucdxml/ucd.all.flat.zip"
|
||||||
UCD="$HOME/.local/share/emoji/ucd.all.flat.zip"
|
DIR="$HOME/.local/share/emoji"
|
||||||
|
LIST="$DIR/emojis.txt.gz"
|
||||||
GREP="rg"
|
GREP="rg"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
|
@ -18,22 +19,29 @@ usage() {
|
||||||
echo " -l LIMIT limit number of output lines"
|
echo " -l LIMIT limit number of output lines"
|
||||||
echo " -u unique result (equals -n and -l 1), no new line"
|
echo " -u unique result (equals -n and -l 1), no new line"
|
||||||
echo " -c show code point"
|
echo " -c show code point"
|
||||||
echo " -d download UCD zip (requires curl)"
|
echo " -z use fzf mode"
|
||||||
|
echo " -d download UCD zip to create list file (requires curl)"
|
||||||
}
|
}
|
||||||
|
|
||||||
[ $# -eq 0 ] && usage && exit
|
[ $# -eq 0 ] && usage && exit
|
||||||
|
|
||||||
download_ucdxml() {
|
download_ucdxml() {
|
||||||
directory="$(dirname "$UCD")"
|
[ ! -d "$DIR" ] && mkdir -p "$DIR"
|
||||||
[ ! -d "$directory" ] && mkdir -p "$directory"
|
unc_list="${LIST%.gz}"
|
||||||
curl -L -o "$UCD" "$UCD_URL"
|
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
HIDE_NAME=
|
HIDE_NAME=
|
||||||
LIMIT=
|
LIMIT=
|
||||||
NO_NEW_LINE=
|
NO_NEW_LINE=
|
||||||
SHOW_CP=
|
SHOW_CP=
|
||||||
while getopts "hdnl:uc" OPTION; do
|
FZF_MODE=
|
||||||
|
while getopts "hdnl:ucz" OPTION; do
|
||||||
case $OPTION in
|
case $OPTION in
|
||||||
h) usage; exit 0 ;;
|
h) usage; exit 0 ;;
|
||||||
d) download_ucdxml; exit $? ;;
|
d) download_ucdxml; exit $? ;;
|
||||||
|
@ -41,28 +49,27 @@ while getopts "hdnl:uc" OPTION; do
|
||||||
l) LIMIT=$OPTARG ;;
|
l) LIMIT=$OPTARG ;;
|
||||||
u) HIDE_NAME=true; LIMIT=1; NO_NEW_LINE=true ;;
|
u) HIDE_NAME=true; LIMIT=1; NO_NEW_LINE=true ;;
|
||||||
c) SHOW_CP=true ;;
|
c) SHOW_CP=true ;;
|
||||||
|
z) FZF_MODE=true ;;
|
||||||
*) usage; exit 1 ;;
|
*) usage; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $(( OPTIND - 1 ))
|
shift $(( OPTIND - 1 ))
|
||||||
FILTER="$*"
|
FILTER="$*"
|
||||||
|
|
||||||
if [ ! -f "$UCD" ]; then
|
if [ ! -f "$LIST" ]; then
|
||||||
echo "Can't find UCD archive at $UCD. Use -d to download it!"
|
echo "Can't find list file at $LIST. Use -d to download it!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
search_chars() {
|
find_emojis() {
|
||||||
zcat "$UCD" | "$GREP" 'Emoji="Y"' | "$GREP" -i "na.?=\"[^\"]*$1[^\"]*\""
|
|
||||||
}
|
|
||||||
|
|
||||||
line_id=0
|
line_id=0
|
||||||
search_chars "$FILTER" | while read -r line; do
|
zcat "$LIST" | "$GREP" -i "$FILTER" | while read -r line; do
|
||||||
[ -n "$LIMIT" ] && (( line_id >= LIMIT )) && break
|
[ -n "$LIMIT" ] && (( line_id >= LIMIT )) && break
|
||||||
codepoint="$(echo "$line" | sed -E 's/.* cp="([0-9A-F]+)".*/\1/g')"
|
readarray -d ";" -t elements <<< "$line"
|
||||||
|
codepoint="${elements[0]}"
|
||||||
result="$(echo -e "\\U$codepoint")"
|
result="$(echo -e "\\U$codepoint")"
|
||||||
if [ "$HIDE_NAME" != true ]; then
|
if [ "$HIDE_NAME" != true ]; then
|
||||||
name="$(echo "$line" | sed -E 's/.* na="([^"]+)".*/\1/g')"
|
name="${elements[1]}"
|
||||||
result="$result $(echo "$name" | tr '[:upper:]' '[:lower:]')"
|
result="$result $(echo "$name" | tr '[:upper:]' '[:lower:]')"
|
||||||
fi
|
fi
|
||||||
if [ "$SHOW_CP" = true ]; then
|
if [ "$SHOW_CP" = true ]; then
|
||||||
|
@ -72,3 +79,15 @@ search_chars "$FILTER" | while read -r line; do
|
||||||
echo "$echo_opt" "$result"
|
echo "$echo_opt" "$result"
|
||||||
line_id=$(( line_id + 1 ))
|
line_id=$(( line_id + 1 ))
|
||||||
done
|
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)"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
find_emojis
|
||||||
|
fi
|
||||||
|
|
Loading…
Reference in a new issue