From 80aa1954df73bbe0b238cd2fd254cb179dbe3b0e Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Sun, 31 Jul 2022 12:22:54 +0200 Subject: Updated emoji.sh to allow groups and to sort emojis by usage. Created script to get emoji file from Unicode official list. --- emoji/emoji.sh | 73 +++++++++++++++++++++++++++++++++++++++++++++++---- emoji/getEmojiList.sh | 28 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) create mode 100755 emoji/getEmojiList.sh diff --git a/emoji/emoji.sh b/emoji/emoji.sh index dfa22cb..09eec51 100755 --- a/emoji/emoji.sh +++ b/emoji/emoji.sh @@ -1,9 +1,72 @@ #!/bin/sh -# Feeds an emoji list into dmenu and allows to select one into clipboard +# Feeds an emoji list into dmenu and allows to select one into clipboard. +# The emoji list file is set as the emojiFile variable of this script. +# +# Usage: emoji.sh [-g] -file="$XDG_CONFIG_HOME/emoji" -selected="$(sed '/^~/d' "$file" | dmenu -i -l 20 -p "Choose an emoji:" | cut -d' ' -f1)" -[ -z "$selected" ] && dunstify -t 2000 "No emoji selected." && exit 0 -echo "$selected" | tr -d '\n' | xsel -ib +usageMsg='Usage: emoji.sh [-g]' + +[ "$1" != '-g' ] && [ -n "$1" ] && echo "$usageMsg" && exit 1 + +emojiFile="$XDG_CONFIG_HOME/emoji" +dmenuPath='/usr/local/bin/dmenu' +groupLineRE='^# group: ' +sedFilter="/${groupLineRE}/d" + +# Check if dmenu has -g option (to show options in a grid) +dmenuGridOptions='' +dmenu -g 4 -v >/dev/null 2>/dev/null && dmenuGridOptions='-g 4' + +byGroups=0 +[ "$1" = '-g' ] && byGroups=1 + +if [ "$byGroups" -eq 1 ]; then + # Select a group name from the file's group lines + group="$(grep "$groupLineRE" "$emojiFile" | + sed "s/${groupLineRE}//" | + dmenu -l 10 -p 'Emoji group:')" + [ -z "$group" ] && exit 0 + + groupLine="$(grep "$group" "$emojiFile")" + + # Prepare the sed filter to delete lines not in the group + sedFilter="/${groupLine}/,/${groupLineRE}/!d;/${groupLineRE}/d" +fi + +# Repeat used emojis at the start of the list sorted by use +emojiList="$(sed "$sedFilter" "$emojiFile")" +usedEmojis="$(echo "$emojiList" | grep '^[0-9]' | sort -nr)" +[ -n "$usedEmojis" ] && + emojiList="$(printf -- \ + "---------Used----------\n${usedEmojis}\n----------All----------\n${emojiList}")" + +# Choose an emoji with dmenu +lineNoNumber="$(echo "$emojiList" | + sed 's/^[ 0-9]*//' | + "$dmenuPath" -i -l 40 $dmenuGridOptions -p "Emoji:")" + +if [ -z "$lineNoNumber" ] || echo "$lineNoNumber" | grep -q '^-'; then + exit 0 +fi + +# Get the usage number +line="$(grep "$lineNoNumber" "$emojiFile")" +number="$(echo "$line" | grep -o '^[0-9]*')" +if [ -n "$number" ] ; then + number=$((number + 1)) +else + number=1 +fi + +# Write the usage number to the emoji file +sed -i "/${line}/s/^[ 0-9]*/${number} /" "$emojiFile" + +# Put the emoji in the clipboard +echo "$lineNoNumber" | + cut -d' ' -f1 | + tr -d '\n' | + xsel -bi + +# Notify the copied emoji dunstify -t 2000 "Copied $(xsel -ob)" diff --git a/emoji/getEmojiList.sh b/emoji/getEmojiList.sh new file mode 100755 index 0000000..f1842db --- /dev/null +++ b/emoji/getEmojiList.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +# Creates a file for emoji.sh to use from an official Unicode emoji list: +# https://unicode.org/Public/emoji/15.0/emoji-test.txt +# +# Usage: getEmojiList.sh [INPUT_FILE] +# +# Example: getEmojiList.sh "$HOME/.config/emoji" + +inputFile="$1" + +sed ' +# Delete not fully qualified emojis +/unqualified/d +/minimally-qualified/d + +# Delete comments except for group names +/^# group:/p +/^#/d + +# Delete blank lines +/^\s*$/d + + +# Leave only emoji and name +s/^[^#].*# //;/^#/!s/E[0-9.]* // + +' "${inputFile:-/dev/stdin}" -- cgit v1.2.1