From e14010eb5302ca3f6076342d95e91915c9861b02 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 11 Jan 2024 10:58:11 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Print=20user=20messages=20to=20s?= =?UTF-8?q?tderr=20(#27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, any messages only intended for the user (status messages, warnings, errors) were sent over stdout. The actual emoji items are also sent to the pickers over stdout so they would sometimes pick up the messages and show them as an option to pick. If history is enabled, we then also save the falsely included items there to forever display as an option. This commit changes internal messages to all be sent over stderr instead, so they should not be picked up by any external programs we hand off to. --- CHANGELOG.md | 1 + bemoji | 19 +++++++++++------- test/bemoji_errors.bats | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 test/bemoji_errors.bats diff --git a/CHANGELOG.md b/CHANGELOG.md index 75d5b7f..88968a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Always download from newest emoji list url - Pass selection to custom typing tools through stdin +- Pass info messages to stderr to avoid passing to picker tools diff --git a/bemoji b/bemoji index 7574a4d..433ffb0 100755 --- a/bemoji +++ b/bemoji @@ -59,6 +59,11 @@ version() { exit } +msg() { +# Outputs a message to stderr, to be used for info, warning and error messages. + printf "%s\n" "$1" >&2 +} + parse_cli() { while getopts cD:ef:hnpP:tv-: arg "$@"; do case "$arg" in @@ -130,7 +135,7 @@ prepare_db() { if [ -n "$BEMOJI_DOWNLOAD_LIST" ]; then # Populate default lists if echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'none'; then - printf "Not downloading a default emoji list.\n" + msg "Not downloading a default emoji list." return elif echo "$BEMOJI_DOWNLOAD_LIST" | grep -q -e 'all'; then dl_default_emoji @@ -158,19 +163,19 @@ dl_default_emoji() { local emojis emojis=$(curl -sSL "https://unicode.org/Public/emoji/latest/emoji-test.txt") printf "%s" "$emojis" | sed -ne 's/^.*; fully-qualified.*# \(\S*\) \S* \(.*$\)/\1 \2/gp' >"$bm_db_location/emojis.txt" - printf "Downloaded default emoji set.\n" + msg "Downloaded default emoji set." } dl_math_symbols() { curl -sSL "https://unicode.org/Public/math/latest/MathClassEx-15.txt" | grep -ve '^#' | cut -d';' -f3,7 | sed -e 's/;/ /' >"$bm_db_location/math.txt" - printf "Downloaded math symbols set.\n" + msg "Downloaded math symbols set." } dl_nerd_symbols() { local nerd all nerd=$(curl -sSL "https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/css/nerd-fonts-generated.css") all+=$(printf "%s" "$nerd" | sed -ne '/\.nf-/p' -e '/\s*[^_]content:/p' | sed -e 'N;s/^\.nf-\(.*\):before.* content: \"\\\(.*\)\";/\\U\2 \1/') echo -e "$all" > "$bm_db_location/nerdfont.txt" - printf "Downloaded nerdfont symbols set.\n" + msg "Downloaded nerdfont symbols set." } gather_emojis() { @@ -231,7 +236,7 @@ _clipper() { elif [ -n "$DISPLAY" ] && command -v xsel >/dev/null 2>&1; then xsel -b else - printf "No suitable clipboard tool found." + msg "No suitable clipboard tool found." exit 1 fi } @@ -250,7 +255,7 @@ _typer() { elif [ -n "$DISPLAY" ] && command -v xdotool >/dev/null 2>&1; then xdotool type --delay 30 "$totype" else - printf "No suitable typing tool found." + msg "No suitable typing tool found." exit 1 fi } @@ -270,7 +275,7 @@ _picker() { fi done - printf "No suitable picker tool found." 1>&2 + msg "No suitable picker tool found." exit 1 } diff --git a/test/bemoji_errors.bats b/test/bemoji_errors.bats new file mode 100644 index 0000000..6345e72 --- /dev/null +++ b/test/bemoji_errors.bats @@ -0,0 +1,43 @@ +#!/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' + + # set up small default set of test emoji for each test + export BEMOJI_DB_LOCATION="$BATS_TEST_TMPDIR/database" + export BEMOJI_HISTORY_LOCATION="$BATS_TEST_TMPDIR/history" + mkdir -p "$BEMOJI_DB_LOCATION" "$BEMOJI_HISTORY_LOCATION" + cat "$BATS_TEST_DIRNAME/resources/test_emoji.txt" > "$BEMOJI_DB_LOCATION/emoji.txt" + + # these tests require stdout to be separated from stderr + # such run flags were only introduced in recent bats version + bats_require_minimum_version 1.5.0 +} + +@test "Prints clipper error to stderr" { + BEMOJI_PICKER_CMD="echo hi" run --separate-stderr bemoji 3>&- + assert_output "" + output="$stderr" + assert_output "No suitable clipboard tool found." +} + +@test "Prints picker error to stderr" { + run --separate-stderr bemoji 3>&- + assert_output "" + output="$stderr" + assert_output "No suitable picker tool found." +} + +@test "Prints typer error to stderr" { + BEMOJI_PICKER_CMD="echo hi" run --separate-stderr bemoji -t 3>&- + assert_output "" + output="$stderr" + assert_output "No suitable typing tool found." +}