From 7e8fc0c51a6eb27d370c3d6460c9eace9559cab8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 10:29:31 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ability=20to=20execute=20mult?= =?UTF-8?q?iple=20command=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- bemoji | 27 +++++++++++++++++---------- test/bemoji_cmds.bats | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/bemoji b/bemoji index da39218..90ebf57 100755 --- a/bemoji +++ b/bemoji @@ -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 diff --git a/test/bemoji_cmds.bats b/test/bemoji_cmds.bats index ac1c3a4..3f7f681 100644 --- a/test/bemoji_cmds.bats +++ b/test/bemoji_cmds.bats @@ -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" +}