Add ability to execute multiple command options

Can combine command options (-c, -t, -e) in any way to simultaneously
clip, type and/or echo the picked results.
Still defaults to clipping, as before.

If a custom default command is given with `BEMOJI_DEFAULT_CMD` env var,
then invoking bemoji without any command option will invoke this
command. Otherwise, the other commands are added to the default given
command.

Additionally, refactor echo command to mirror other commands
Both typing and clipping simply invoke the corresponding program
function while echo had an extra role so far. This adjusts the echo
command to also work the same way.
This commit is contained in:
Marty Oehme 2022-08-06 10:29:31 +02:00
parent 28aa2aa522
commit 7e8fc0c51a
Signed by: Marty
GPG key ID: B7538B8F50A1C800
2 changed files with 34 additions and 10 deletions

27
bemoji
View file

@ -10,9 +10,6 @@ bm_db_location=${BEMOJI_DB_LOCATION:-"${XDG_DATA_HOME:-$HOME/.local/share}/bemoj
bm_cache_dir="${BEMOJI_CACHE_LOCATION:-${XDG_CACHE_HOME:-$HOME/.cache}}"
bm_history_file="${bm_cache_dir}/bemoji-history.txt"
# Command to run after user chooses an emoji
bm_default_cmd=${BEMOJI_DEFAULT_CMD:-'_clipResult'}
# Do not save choices
bm_private_mode=${BEMOJI_PRIVATE_MODE:-false}
# Do not sort results
@ -48,9 +45,9 @@ version() {
while getopts ":f:D:tcepPhv" o; do
case "${o}" in
f) BEMOJI_CUSTOM_LIST="${OPTARG}" ;;
t) bm_default_cmd=_typeResult ;;
c) bm_default_cmd=_clipResult ;;
e) bm_default_cmd=_echo ;;
t) bm_default_cmd+=(_typeResult) ;;
c) bm_default_cmd+=(_clipResult) ;;
e) bm_default_cmd+=(_echoResult) ;;
D) BEMOJI_DOWNLOAD_LIST="${OPTARG}" ;;
p) bm_private_mode=true ;;
P) bm_ignore_recent=true ;;
@ -195,6 +192,10 @@ _clipResult() {
cat - | _clipper
}
_echoResult() {
cat -
}
[ -n "$BEMOJI_CUSTOM_LIST" ] || prepare_db
result=$(gather_emojis | _picker)
exit_value="$?"
@ -206,11 +207,17 @@ case "$exit_value" in
exit
;;
0)
if [ "$bm_default_cmd" = "_echo" ]; then
echo "$result"
exit
if [ ${#bm_default_cmd[@]} -eq 0 ]; then
if [ -n "$BEMOJI_DEFAULT_CMD" ]; then
# shellcheck disable=SC2068
echo "$result" | ${BEMOJI_DEFAULT_CMD[@]}
exit
fi
bm_default_cmd+=(_clipResult)
fi
echo "$result" | "$bm_default_cmd"
for cmd in "${bm_default_cmd[@]}"; do
echo "$result" | "$cmd"
done
;;
10)
echo "$result" | _clipResult

View file

@ -46,3 +46,20 @@ setup() {
BEMOJI_TYPE_CMD="echo typing result" run bemoji -t 3>&-
assert_output "typing result"
}
@test "Runs typing and clipping on -ct options" {
BEMOJI_CLIP_CMD="echo clipping result" BEMOJI_TYPE_CMD="echo typing result" run bemoji -ct 3>&-
assert_output \
"clipping result
typing result"
}
@test "Runs custom default command" {
BEMOJI_DEFAULT_CMD="echo my custom command" run bemoji 3>&-
assert_output "my custom command"
}
@test "Using command option overrides custom default command" {
BEMOJI_DEFAULT_CMD="echo my custom command" BEMOJI_CLIP_CMD="echo my clipping" run bemoji -c 3>&-
assert_output "my clipping"
}