2021-12-01 21:12:31 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-12-01 14:51:37 +00:00
|
|
|
|
2022-06-29 10:32:13 +00:00
|
|
|
bm_version=0.2.0
|
2021-12-01 14:51:37 +00:00
|
|
|
# Emoji default database location
|
|
|
|
bm_db_location=${BEMOJI_DB_LOCATION:-"${XDG_DATA_HOME:-$HOME/.local/share}/bemoji"}
|
|
|
|
# Setting custom emoji list file location:
|
|
|
|
# BEMOJI_CUSTOM_LIST=/my/location/emojis.txt
|
2022-11-03 14:35:06 +00:00
|
|
|
# Setting custom recent emoji history:
|
|
|
|
# BEMOJI_HISTORY_LOCATION=/path/to/my/recents/directory
|
|
|
|
bm_state_dir="${BEMOJI_HISTORY_LOCATION:-${XDG_STATE_HOME:-$HOME/.local/state}}"
|
|
|
|
bm_history_file="${bm_state_dir}/bemoji-history.txt"
|
2021-12-01 14:51:37 +00:00
|
|
|
|
2022-08-06 09:18:06 +00:00
|
|
|
# Command to run after user chooses an emoji
|
|
|
|
bm_default_cmd="$BEMOJI_DEFAULT_CMD"
|
|
|
|
|
2022-09-27 19:01:44 +00:00
|
|
|
# Newline after echo
|
|
|
|
bm_echo_newline=${BEMOJI_ECHO_NEWLINE:-true}
|
2021-12-01 14:51:37 +00:00
|
|
|
# Do not save choices
|
|
|
|
bm_private_mode=${BEMOJI_PRIVATE_MODE:-false}
|
|
|
|
# Do not sort results
|
|
|
|
bm_ignore_recent=${BEMOJI_IGNORE_RECENT:-false}
|
|
|
|
|
|
|
|
# Report usage
|
|
|
|
usage() {
|
2021-12-01 21:15:30 +00:00
|
|
|
echo "Usage: $(basename "$0") [-t | -c | -e] [-f <filepath> ] [-p] [-P] [-D <choices>]" 1>&2
|
2021-12-01 14:51:37 +00:00
|
|
|
echo
|
|
|
|
echo "A simple emoji picker. Runs on bemenu/wofi/rofi/dmenu by default."
|
|
|
|
echo "Invoked without arguments sends the picked emoji to the clipboard."
|
|
|
|
echo
|
2022-08-06 09:39:52 +00:00
|
|
|
echo " Command options (can be combined):"
|
2021-12-01 14:51:37 +00:00
|
|
|
echo " -t Simulate typing the emoji choice with the keyboard."
|
|
|
|
echo " -c Send emoji choice to the clipboard. (default)"
|
2022-08-06 09:39:52 +00:00
|
|
|
echo " -e Only echo out the picked emoji."
|
|
|
|
echo ""
|
|
|
|
echo " Other options:"
|
2022-09-27 19:01:44 +00:00
|
|
|
echo " -n Do not print a newline after the picked emoji."
|
2021-12-01 14:51:37 +00:00
|
|
|
echo " -p Do not save picked emoji to recent history."
|
|
|
|
echo " -P Do not order emoji by recently used."
|
|
|
|
echo " -D <choice> Choose specific default lists to download if none found locally."
|
2021-12-01 21:15:30 +00:00
|
|
|
echo " Valid choices: all|none|emoji|math."
|
2022-08-06 09:39:52 +00:00
|
|
|
echo " -f <filepath> Use a custom emoji database. Can be a url which will be retrieved."
|
2022-06-29 09:44:15 +00:00
|
|
|
echo " -v Display current program version and directory configuration."
|
2022-06-29 06:20:22 +00:00
|
|
|
echo " -h Show this help."
|
2021-12-01 14:51:37 +00:00
|
|
|
echo
|
|
|
|
exit "$1"
|
|
|
|
}
|
|
|
|
|
2021-12-01 21:31:27 +00:00
|
|
|
version() {
|
2022-06-29 09:44:15 +00:00
|
|
|
printf "v%s\ndatabase=%s\nhistory=%s\n" "$bm_version" "$bm_db_location" "$bm_history_file"
|
2021-12-01 21:31:27 +00:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:51:37 +00:00
|
|
|
# Get Options
|
2022-09-27 19:01:44 +00:00
|
|
|
while getopts ":f:D:tcenpPhv" o; do
|
2021-12-01 14:51:37 +00:00
|
|
|
case "${o}" in
|
|
|
|
f) BEMOJI_CUSTOM_LIST="${OPTARG}" ;;
|
2022-08-06 09:18:06 +00:00
|
|
|
t) bm_cmds+=(_typer) ;;
|
|
|
|
c) bm_cmds+=(_clipper) ;;
|
|
|
|
e) bm_cmds+=(cat) ;;
|
2022-11-03 14:35:06 +00:00
|
|
|
n) bm_echo_newline=false ;;
|
2021-12-01 14:51:37 +00:00
|
|
|
D) BEMOJI_DOWNLOAD_LIST="${OPTARG}" ;;
|
|
|
|
p) bm_private_mode=true ;;
|
|
|
|
P) bm_ignore_recent=true ;;
|
|
|
|
h) usage 0 ;;
|
2021-12-01 21:31:27 +00:00
|
|
|
v) version ;;
|
2021-12-01 14:51:37 +00:00
|
|
|
*) usage 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
prepare_db() {
|
|
|
|
# Create list directory
|
|
|
|
if [ ! -d "$bm_db_location" ]; then
|
|
|
|
mkdir -p "$bm_db_location"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$(find "$bm_db_location" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
|
|
|
|
# Populate default lists
|
|
|
|
if echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'none'; then
|
|
|
|
printf "No emoji list found, but set to not download any default lists."
|
|
|
|
exit 1
|
|
|
|
elif echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'all'; then
|
|
|
|
dl_default_emoji
|
|
|
|
dl_math_symbols
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ -z "$BEMOJI_DOWNLOAD_LIST" ] || echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'emoji'; then
|
|
|
|
dl_default_emoji
|
|
|
|
fi
|
|
|
|
if echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'math'; then
|
|
|
|
dl_math_symbols
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
dl_default_emoji() {
|
|
|
|
curl -sSL "https://unicode.org/Public/emoji/14.0/emoji-test.txt" |
|
|
|
|
sed -ne 's/^.*; fully-qualified.*# \(\S*\) \S* \(.*$\)/\1 \2/gp' >"$bm_db_location/emojis.txt"
|
|
|
|
printf "Downloaded default emoji set."
|
|
|
|
}
|
|
|
|
dl_math_symbols() {
|
|
|
|
curl -sSL "https://unicode.org/Public/math/latest/MathClassEx-15.txt" |
|
|
|
|
grep -ve '^#' | cut -d';' -f3,7 --output-delimiter=' ' >"$bm_db_location/math.txt"
|
|
|
|
printf "Downloaded math symbols set."
|
|
|
|
}
|
|
|
|
|
|
|
|
gather_emojis() {
|
|
|
|
if [ -n "$BEMOJI_CUSTOM_LIST" ] && [ -f "$BEMOJI_CUSTOM_LIST" ]; then
|
|
|
|
result=$(cat "$BEMOJI_CUSTOM_LIST")
|
|
|
|
elif [ -n "$BEMOJI_CUSTOM_LIST" ] && curl -fsSI "$BEMOJI_CUSTOM_LIST" >/dev/null 2>&1; then
|
|
|
|
result=$(curl -sSL "$BEMOJI_CUSTOM_LIST")
|
|
|
|
else
|
|
|
|
result=$(cat "$bm_db_location"/*.txt)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$bm_ignore_recent" = true ]; then
|
|
|
|
printf "%s" "$result"
|
|
|
|
else
|
|
|
|
printf "%s\n%s" "$(get_most_recent)" "$result" | cat -n - | sort -uk2 | sort -n | cut -f2-
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_most_recent() {
|
|
|
|
recent_file="$bm_history_file"
|
|
|
|
if [ ! -f "$recent_file" ]; then
|
|
|
|
touch "$recent_file"
|
|
|
|
fi
|
|
|
|
# TODO improve this messy line
|
2021-12-01 21:16:46 +00:00
|
|
|
sed -e '/^$/d' "$recent_file" |
|
2021-12-01 14:51:37 +00:00
|
|
|
sort |
|
|
|
|
uniq -c |
|
|
|
|
sort -rn |
|
|
|
|
sed -e 's/^\s*//' |
|
|
|
|
cut -d' ' -f2-
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_recent() {
|
|
|
|
if [ -z "$1" ]; then return; fi
|
2022-11-03 14:35:06 +00:00
|
|
|
if [ ! -d "$bm_state_dir" ]; then
|
|
|
|
mkdir -p "$bm_state_dir"
|
2021-12-01 14:51:37 +00:00
|
|
|
fi
|
|
|
|
echo "$1" >>"$bm_history_file"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set default clipboard util
|
|
|
|
_clipper() {
|
|
|
|
if [ -n "$BEMOJI_CLIP_CMD" ]; then
|
2022-06-29 07:53:04 +00:00
|
|
|
# shellcheck disable=SC2068
|
|
|
|
${BEMOJI_CLIP_CMD[@]}
|
2021-12-01 14:51:37 +00:00
|
|
|
elif [ -n "$WAYLAND_DISPLAY" ] && command -v wl-copy >/dev/null 2>&1; then
|
|
|
|
wl-copy
|
|
|
|
elif [ -n "$DISPLAY" ] && command -v xclip >/dev/null 2>&1; then
|
|
|
|
xclip -selection clipboard
|
|
|
|
elif [ -n "$DISPLAY" ] && command -v xsel >/dev/null 2>&1; then
|
|
|
|
xsel -b
|
|
|
|
else
|
|
|
|
printf "No suitable clipboard tool found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set default typing uti
|
|
|
|
_typer() {
|
|
|
|
totype=$(cat -)
|
|
|
|
if [ -n "$BEMOJI_TYPE_CMD" ]; then
|
2022-06-29 07:53:04 +00:00
|
|
|
# shellcheck disable=SC2068
|
|
|
|
${BEMOJI_TYPE_CMD[@]}
|
2021-12-01 14:51:37 +00:00
|
|
|
elif [ -n "$WAYLAND_DISPLAY" ] && command -v wtype >/dev/null 2>&1; then
|
|
|
|
wtype -s 30 "$totype"
|
|
|
|
elif [ -n "$DISPLAY" ] && command -v xdotool >/dev/null 2>&1; then
|
2022-01-07 11:10:39 +00:00
|
|
|
xdotool type --delay 30 "$totype"
|
2021-12-01 14:51:37 +00:00
|
|
|
else
|
|
|
|
printf "No suitable typing tool found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set default picker util
|
|
|
|
_picker() {
|
|
|
|
if [ -n "$BEMOJI_PICKER_CMD" ]; then
|
2022-06-29 07:53:04 +00:00
|
|
|
# shellcheck disable=SC2068
|
|
|
|
${BEMOJI_PICKER_CMD[@]}
|
2021-12-01 14:51:37 +00:00
|
|
|
elif command -v bemenu >/dev/null 2>&1; then
|
|
|
|
bemenu -p 🔍 -i -l 20
|
|
|
|
elif command -v wofi >/dev/null 2>&1; then
|
|
|
|
wofi -p 🔍 -i --show dmenu
|
|
|
|
elif command -v rofi >/dev/null 2>&1; then
|
2022-09-09 13:50:17 +00:00
|
|
|
rofi -p 🔍 -i -dmenu --kb-custom-1 "Alt+1" --kb-custom-2 "Alt+2"
|
2021-12-01 14:51:37 +00:00
|
|
|
elif command -v dmenu >/dev/null 2>&1; then
|
|
|
|
dmenu -p 🔍 -i -l 20
|
|
|
|
else
|
|
|
|
printf "No suitable picker tool found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -n "$BEMOJI_CUSTOM_LIST" ] || prepare_db
|
|
|
|
result=$(gather_emojis | _picker)
|
|
|
|
exit_value="$?"
|
|
|
|
[ "$bm_private_mode" = true ] || add_to_recent "$result"
|
2022-06-29 08:08:01 +00:00
|
|
|
result=$(echo "$result" | grep -o '^\S\+' | tr -d '\n')
|
2021-12-01 14:51:37 +00:00
|
|
|
|
|
|
|
case "$exit_value" in
|
|
|
|
1)
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
0)
|
2022-08-06 09:18:06 +00:00
|
|
|
if [ ${#bm_cmds[@]} -eq 0 ]; then
|
|
|
|
if [ -n "$bm_default_cmd" ]; then
|
2022-08-06 08:29:31 +00:00
|
|
|
# shellcheck disable=SC2068
|
2022-08-06 09:18:06 +00:00
|
|
|
echo "$result" | ${bm_default_cmd[@]}
|
2022-08-06 08:29:31 +00:00
|
|
|
exit
|
|
|
|
fi
|
2022-08-06 09:18:06 +00:00
|
|
|
bm_cmds+=(_clipper)
|
2021-12-01 14:51:37 +00:00
|
|
|
fi
|
2022-08-06 09:18:06 +00:00
|
|
|
for cmd in "${bm_cmds[@]}"; do
|
2022-09-27 19:01:44 +00:00
|
|
|
[ "$bm_echo_newline" = true ] && echo_opts= || echo_opts=-n
|
|
|
|
echo $echo_opts "$result" | "$cmd"
|
2022-08-06 08:29:31 +00:00
|
|
|
done
|
2021-12-01 14:51:37 +00:00
|
|
|
;;
|
|
|
|
10)
|
2022-08-06 09:18:06 +00:00
|
|
|
echo "$result" | _clipper
|
2021-12-01 14:51:37 +00:00
|
|
|
;;
|
|
|
|
11)
|
2022-08-06 09:18:06 +00:00
|
|
|
echo "$result" | _typer
|
2021-12-01 14:51:37 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
exit
|