From aed7176e73762e68287c4ba415edefc5f8c96acd Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Tue, 30 Jul 2019 19:35:24 +0100 Subject: [PATCH 1/3] Add support for custom message formats --- README.rst | 15 +++++++++++++++ auto-notify.plugin.zsh | 27 ++++++++++++++++++++++----- tests/test_auto_notify_format.zunit | 26 ++++++++++++++++++++++++++ tests/test_auto_notify_send.zunit | 14 ++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 tests/test_auto_notify_format.zunit 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" +} From 7721d45abe272e0de8ceb9242fc76609f4cc4377 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Wed, 31 Jul 2019 13:37:47 +0100 Subject: [PATCH 2/3] Bump version to 0.5.0 --- CHANGELOG.md | 4 ++++ auto-notify.plugin.zsh | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04929cb..9bacfea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog for zsh-auto-notify ============================= +0.5.0 +----- +* Support changing notification title and body using AUTO_NOTIFY_TITLE and AUTO_NOTIFY_BODY + 0.4.0 ----- * Add `AUTO_NOTIFY_EXPIRE_TIME` configuration option diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 5b57d64..673e66b 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -1,4 +1,4 @@ -export AUTO_NOTIFY_VERSION="0.4.0" +export AUTO_NOTIFY_VERSION="0.5.0" # Time it takes for a notification to expire export AUTO_NOTIFY_EXPIRE_TIME=8000 From d039eea61bb99bb4258155340dea758946db0f9a Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Wed, 31 Jul 2019 13:40:01 +0100 Subject: [PATCH 3/3] Fix to README for notification formatting --- README.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index d37b48d..e4ec9ff 100644 --- a/README.rst +++ b/README.rst @@ -99,13 +99,14 @@ You can change the formatting of notifications by setting the values for ``AUTO_ ``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 +* ``%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"