From 28aa2aa5224b3fcc2e125d8703b61a11b080d79a Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 09:42:48 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=A7=AA=20Add=20tests=20for=20clipping?= =?UTF-8?q?,=20echo=20and=20typing=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a simple test for each of the base command options: echo, type, clip. It uses custom commands set through environment options so does not yet test the actual default command logic (finding a typer, clipper), that is still a TODO. Also split tests into tests concerning commands of the program and tests concerning the directory settings and options of the program, to avoid ending up with a single test file spanning hundreds of lines. --- test/bemoji_cmds.bats | 48 +++++++++++++++++++ test/{bemoji.bats => bemoji_directories.bats} | 12 ----- 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 test/bemoji_cmds.bats rename test/{bemoji.bats => bemoji_directories.bats} (88%) diff --git a/test/bemoji_cmds.bats b/test/bemoji_cmds.bats new file mode 100644 index 0000000..ac1c3a4 --- /dev/null +++ b/test/bemoji_cmds.bats @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +setup_file() { + # make bemoji executable from anywhere relative to current testfile + TEST_DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + PATH="$TEST_DIR/..:$PATH" +} + +setup() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + + # mock out interactive picker for static emoji return + export BEMOJI_PICKER_CMD="echo ❤️" + + # set up small default set of test emoji for each test + export BEMOJI_DB_LOCATION="$BATS_TEST_TMPDIR/database" + export BEMOJI_CACHE_LOCATION="$BATS_TEST_TMPDIR/cache" + mkdir -p "$BEMOJI_DB_LOCATION" "$BEMOJI_CACHE_LOCATION" + cat "$BATS_TEST_DIRNAME/resources/test_emoji.txt" > "$BEMOJI_DB_LOCATION/emoji.txt" +} + +@test "-v prints correct version number" { + the_version=$(grep 'bm_version=' "$(which bemoji)") + + run bemoji -v + assert_output --partial "v${the_version#bm_version=}" +} + +@test "Runs clipping command by default" { + BEMOJI_CLIP_CMD="echo clipping default" run bemoji 3>&- + assert_output "clipping default" +} + +@test "Runs echo command on -e option" { + run bemoji -e 3>&- + assert_output "❤️" +} + +@test "Runs clipping command on -c option" { + BEMOJI_CLIP_CMD="echo clipping result" run bemoji -c 3>&- + assert_output "clipping result" +} + +@test "Runs typing command on -t option" { + BEMOJI_TYPE_CMD="echo typing result" run bemoji -t 3>&- + assert_output "typing result" +} diff --git a/test/bemoji.bats b/test/bemoji_directories.bats similarity index 88% rename from test/bemoji.bats rename to test/bemoji_directories.bats index cff3af6..28068e1 100644 --- a/test/bemoji.bats +++ b/test/bemoji_directories.bats @@ -28,18 +28,6 @@ setup() { assert_success } -@test "can receive custom picker mock output" { - run bemoji -e 3>&- - assert_output "❤️" -} - -@test "-v prints correct version number" { - the_version=$(grep 'bm_version=' $(which bemoji)) - - run bemoji -v - assert_output --partial "v${the_version#bm_version=}" -} - @test "sets XDG directory for db by default" { unset BEMOJI_DB_LOCATION export XDG_DATA_HOME="$BATS_TEST_TMPDIR/xdb-db" From 7e8fc0c51a6eb27d370c3d6460c9eace9559cab8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 10:29:31 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20Add=20ability=20to=20execute=20?= =?UTF-8?q?multiple=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" +} From a776c6374873ebe9f1e351a21f6c4d53f69107d4 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 11:18:06 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=A6=8A=20Refactor=20command=20functio?= =?UTF-8?q?ns=20and=20variable=20names?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed redundant 'redirection' functions which did nothing to change code and only extended the pipe with an additional `cat` call. Renamed bm_default_cmd to bm_cmds to reflect its nature as an array. Since there is not just one command contained in the variable anymore, it makes much more sense to now call it bm_cmds, note the plural. --- bemoji | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/bemoji b/bemoji index 90ebf57..431ba41 100755 --- a/bemoji +++ b/bemoji @@ -10,6 +10,9 @@ 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" + # Do not save choices bm_private_mode=${BEMOJI_PRIVATE_MODE:-false} # Do not sort results @@ -45,9 +48,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+=(_echoResult) ;; + t) bm_cmds+=(_typer) ;; + c) bm_cmds+=(_clipper) ;; + e) bm_cmds+=(cat) ;; D) BEMOJI_DOWNLOAD_LIST="${OPTARG}" ;; p) bm_private_mode=true ;; P) bm_ignore_recent=true ;; @@ -183,19 +186,6 @@ _picker() { fi } -# Type result using xdotool -_typeResult() { - cat - | _typer -} - -_clipResult() { - cat - | _clipper -} - -_echoResult() { - cat - -} - [ -n "$BEMOJI_CUSTOM_LIST" ] || prepare_db result=$(gather_emojis | _picker) exit_value="$?" @@ -207,23 +197,23 @@ case "$exit_value" in exit ;; 0) - if [ ${#bm_default_cmd[@]} -eq 0 ]; then - if [ -n "$BEMOJI_DEFAULT_CMD" ]; then + if [ ${#bm_cmds[@]} -eq 0 ]; then + if [ -n "$bm_default_cmd" ]; then # shellcheck disable=SC2068 - echo "$result" | ${BEMOJI_DEFAULT_CMD[@]} + echo "$result" | ${bm_default_cmd[@]} exit fi - bm_default_cmd+=(_clipResult) + bm_cmds+=(_clipper) fi - for cmd in "${bm_default_cmd[@]}"; do + for cmd in "${bm_cmds[@]}"; do echo "$result" | "$cmd" done ;; 10) - echo "$result" | _clipResult + echo "$result" | _clipper ;; 11) - echo "$result" | _typeResult + echo "$result" | _typer ;; esac exit From 05f0e71b9f78cefb9e8701fcdb5bb70fb07bf033 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 6 Aug 2022 11:39:52 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=96=20Update=20CHANGELOG=20and=20a?= =?UTF-8?q?dd=20explanation=20to=20usage=20help?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added quick note that commands can be used in combination and separated commands from other options to better highlight them. --- CHANGELOG.md | 8 ++++++-- bemoji | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6103f22..8f92f22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,13 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - +### Changed + +- Multiple command options can be combined - +### Fixed + +- Custom default command is only executed when no command option given diff --git a/bemoji b/bemoji index 431ba41..9801092 100755 --- a/bemoji +++ b/bemoji @@ -25,14 +25,17 @@ usage() { 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 - echo " -f Use a custom emoji database. Can be a url which will be retrieved." + echo " Command options (can be combined):" echo " -t Simulate typing the emoji choice with the keyboard." echo " -c Send emoji choice to the clipboard. (default)" + echo " -e Only echo out the picked emoji." + echo "" + echo " Other options:" echo " -p Do not save picked emoji to recent history." echo " -P Do not order emoji by recently used." - echo " -e Only echo out the picked emoji." echo " -D Choose specific default lists to download if none found locally." echo " Valid choices: all|none|emoji|math." + echo " -f Use a custom emoji database. Can be a url which will be retrieved." echo " -v Display current program version and directory configuration." echo " -h Show this help." echo