diff --git a/README.rst b/README.rst index 0991cb2..d37b48d 100644 --- a/README.rst +++ b/README.rst @@ -93,6 +93,21 @@ can configure this value by setting the environment variable ``AUTO_NOTIFY_THRES # Set threshold to 20seconds export AUTO_NOTIFY_THRESHOLD=20 +**Notification Formatting** + +You can change the formatting of notifications by setting the values for ``AUTO_NOTIFY_TITLE`` and +``AUTO_NOTIFY_BODY``. When writing these values, the following variables will be replaced according to +the data that ``auto-notify`` has detected: + +* `%command` - the command that the user executed +* `%elapsed` - number of seconds that elapsed +* `%exit_code` - the exit code of the command that was executed + +An example of how these values can be set is shown below: + +:: + export AUTO_NOTIFY_TITLE="Hey! %command has just finished" + export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code" **Notification Expiration Time** diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 7487ebf..5b57d64 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -9,26 +9,43 @@ export AUTO_NOTIFY_IGNORE=( "vim" "nvim" "less" "more" "man" "tig" "watch" "git commit" "top" "htop" "ssh" "nano" ) +function _auto_notify_format() { + local MESSAGE="$1" + local command="$2" + local elapsed="$3" + local exit_code="$4" + MESSAGE="${MESSAGE//\%command/$command}" + MESSAGE="${MESSAGE//\%elapsed/$elapsed}" + MESSAGE="${MESSAGE//\%exit_code/$exit_code}" + printf "%s" "$MESSAGE" +} + function _auto_notify_message() { local command="$1" local elapsed="$2" local exit_code="$3" local platform="$(uname)" # Run using echo -e in order to make sure notify-send picks up new line - local title="\"$command\" Completed" - local text="$(echo -e "Total time: $elapsed seconds\nExit code: $exit_code")" + local DEFAULT_TITLE="\"%command\" Completed" + local DEFAULT_BODY="$(echo -e "Total time: %elapsed seconds\nExit code: %exit_code")" + + local title="${AUTO_NOTIFY_TITLE:-$DEFAULT_TITLE}" + local text="${AUTO_NOTIFY_BODY:-$DEFAULT_BODY}" + + title="$(_auto_notify_format "$title" "$command" "$elapsed" "$exit_code")" + body="$(_auto_notify_format "$text" "$command" "$elapsed" "$exit_code")" if [[ "$platform" == "Linux" ]]; then local urgency="normal" if [[ "$exit_code" != "0" ]]; then urgency="critical" fi - notify-send "$title" "$text" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME" + notify-send "$title" "$body" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME" elif [[ "$platform" == "Darwin" ]]; then # We need to escape quotes since we are passing a script into a command - text="${text//\"/\\\"}" + body="${body//\"/\\\"}" title="${title//\"/\\\"}" - osascript -e "display notification \"$text\" with title \"$title\"" + osascript -e "display notification \"$body\" with title \"$title\"" else printf "Unknown platform for sending notifications: $platform\n" printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n" diff --git a/tests/test_auto_notify_format.zunit b/tests/test_auto_notify_format.zunit new file mode 100644 index 0000000..259238e --- /dev/null +++ b/tests/test_auto_notify_format.zunit @@ -0,0 +1,26 @@ +#!/usr/bin/env zunit + +@setup { + load "../auto-notify.plugin.zsh" +} + +@test 'auto-notify-format no parameters' { + run _auto_notify_format "Hello World" "foo" "bar" "baz" + + assert $state equals 0 + assert "$output" same_as "Hello World" +} + +@test 'auto-notify-format correct formatting 1' { + run _auto_notify_format "Command run is - %command (%elapsed seconds)" "zypper up" "10" "0" + + assert $state equals 0 + assert "$output" same_as "Command run is - zypper up (10 seconds)" +} + +@test 'auto-notify-format correct formatting 2' { + run _auto_notify_format "Exit code is %exit_code!" "zypper up" "10" "-101" + + assert $state equals 0 + assert "$output" same_as "Exit code is -101!" +} diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 1cb14a2..2e0bb2c 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -126,3 +126,17 @@ assert "$lines[1]" same_as "Unknown platform for sending notifications: Hal9000" assert "$lines[2]" same_as "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/" } + +@test 'auto-notify-send sends custom message' { + AUTO_COMMAND="doom -i" + AUTO_COMMAND_FULL="doom -i" + AUTO_COMMAND_START=11055 + AUTO_NOTIFY_TITLE="%command has completed in %elapseds yo" + AUTO_NOTIFY_BODY="%command exited with code %exit_code" + + run _auto_notify_send + + assert $state equals 0 + assert "$lines[1]" same_as 'Notification Title: doom -i has completed in 45s yo' + assert "$lines[2]" same_as "Notification Body: doom -i exited with code 0" +}