From 7b6d59479abad6848a3700363a0331e88fc6b166 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sat, 20 Jul 2019 21:34:17 +0100 Subject: [PATCH 1/5] Rename main test file to test_plugin.zunit --- auto-notify.plugin.zsh | 3 +-- tests/{test_auto_notify.zunit => test_plugin.zunit} | 0 2 files changed, 1 insertion(+), 2 deletions(-) rename tests/{test_auto_notify.zunit => test_plugin.zunit} (100%) diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 0944e38..be07099 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -86,11 +86,10 @@ function disable_auto_notify() { } function enable_auto_notify() { + autoload -Uz add-zsh-hook add-zsh-hook preexec _auto_notify_track add-zsh-hook precmd _auto_notify_send } -autoload -Uz add-zsh-hook - _auto_notify_reset_tracking enable_auto_notify diff --git a/tests/test_auto_notify.zunit b/tests/test_plugin.zunit similarity index 100% rename from tests/test_auto_notify.zunit rename to tests/test_plugin.zunit From cac8315195c07e65f8e5e11d89feaab7225155b8 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sun, 21 Jul 2019 21:50:45 +0100 Subject: [PATCH 2/5] Improvements to notification formatting and options --- auto-notify.plugin.zsh | 20 ++++++++++++++++---- tests/test_auto_notify_send.zunit | 16 ++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index be07099..216501f 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -1,5 +1,7 @@ export AUTO_NOTIFY_VERSION="0.3.0" +# Time it takes for a notification to expire +export AUTO_NOTIFY_EXPIRE_TIME=8000 # Threshold in seconds for when to automatically show a notification export AUTO_NOTIFY_THRESHOLD=10 # List of commands/programs to ignore sending notifications for @@ -10,16 +12,23 @@ export AUTO_NOTIFY_IGNORE=( 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 text="$(echo -e "\"$command\" has completed\n(Total time: $elapsed seconds)")" + local title="\"$command\" Completed" + local text="$(echo -e "Total time: $elapsed seconds\nExit code: $exit_code")" if [[ "$platform" == "Linux" ]]; then - notify-send "$text" + local urgency="normal" + if [[ "$exit_code" != "0" ]]; then + urgency="critical" + fi + notify-send "$title" "$text" "--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//\"/\\\"}" - osascript -e "display notification \"$text\" with title \"Command Completed\"" + title="${title//\"/\\\"}" + osascript -e "display notification \"$text\" 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" @@ -44,6 +53,9 @@ function _is_auto_notify_ignored() { } function _auto_notify_send() { + # Immediately store the exit code before it goes away + local exit_code="$?" + if [[ -z "$AUTO_COMMAND" && -z "$AUTO_COMMAND_START" ]]; then return fi @@ -53,7 +65,7 @@ function _auto_notify_send() { let "elapsed = current - AUTO_COMMAND_START" if [[ $elapsed -gt $AUTO_NOTIFY_THRESHOLD ]]; then - _auto_notify_message "$AUTO_COMMAND" "$elapsed" + _auto_notify_message "$AUTO_COMMAND" "$elapsed" "$exit_code" fi fi diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 8aa96cb..59f844c 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -4,7 +4,9 @@ load "../auto-notify.plugin.zsh" function notify-send { - echo - "Notification: $@" + echo - "Notification Title: $1" + echo - "Notification Body: $2" + echo - "${@:3}" } function uname { @@ -83,8 +85,10 @@ run _auto_notify_send assert $state equals 0 - assert "$lines[1]" same_as 'Notification: "f bar -r" has completed' - assert "$lines[2]" same_as "(Total time: 20 seconds)" + assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed' + assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds" + assert "$lines[3]" same_as "Exit code: 0" + assert "$lines[4]" same_as "--urgency=normal --expire-time=8000" } @test 'auto-notify-send sends notification on MacOSX' { @@ -97,14 +101,14 @@ } function osascript { - echo - $@ + echo - "${@}" } run _auto_notify_send assert $state equals 0 - assert "$lines[1]" same_as '-e display notification "\"f bar -r\" has completed' - assert "$lines[2]" same_as '(Total time: 20 seconds)" with title "Command Completed"' + assert "$lines[1]" same_as '-e display notification "Total time: 20 seconds' + assert "$lines[2]" same_as 'Exit code: 0" with title "\"f bar -r\" Completed"' } @test 'auto-notify-send sends warning on unsupported platform' { From 831c0b0b00f4c68815d6677270f1e787b992289e Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sun, 21 Jul 2019 21:52:58 +0100 Subject: [PATCH 3/5] Add entry in README about AUTO_NOTIFY_EXPIRE_TIME --- README.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.rst b/README.rst index b968616..0991cb2 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,18 @@ can configure this value by setting the environment variable ``AUTO_NOTIFY_THRES export AUTO_NOTIFY_THRESHOLD=20 +**Notification Expiration Time** + +You can set how long a notification sent by ``auto-notify`` will remain showing by setting the environment +variable ``AUTO_NOTIFY_EXPIRE_TIME`` to a custom value in milliseconds. The default value is set to 8 seconds. +NOTE: This configuration option currently only works for Linux. + +:: + + # Set notification expiry to 10 seconds + export AUTO_NOTIFY_EXPIRE_TIME=10000 + + **Ignored Commands** A number of commands do not get notifications for long running times due to their nature (e.g. ``watch`` or ``man``). From 9596b8789993f876c563629ed951f530006146e5 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sun, 21 Jul 2019 21:56:02 +0100 Subject: [PATCH 4/5] Bump version to 0.4.0 --- CHANGELOG.md | 7 +++++++ auto-notify.plugin.zsh | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60a990f..04929cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog for zsh-auto-notify ============================= +0.4.0 +----- +* Add `AUTO_NOTIFY_EXPIRE_TIME` configuration option +* Improvements to notification formatting +* Exit code is now displayed in notifications +* Notifications on linux now show as critical if long command exits with non-zero exit code + 0.3.0 ----- * Add support for environments where standard history is disabled. Fixed in #10 diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 216501f..7487ebf 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -1,4 +1,4 @@ -export AUTO_NOTIFY_VERSION="0.3.0" +export AUTO_NOTIFY_VERSION="0.4.0" # Time it takes for a notification to expire export AUTO_NOTIFY_EXPIRE_TIME=8000 From 87d1e9bd0c2a212d2cd09beea0e86ecfea6ef58d Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Sun, 21 Jul 2019 21:59:14 +0100 Subject: [PATCH 5/5] Test configurable expiry time --- tests/test_auto_notify_send.zunit | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 59f844c..1cb14a2 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -82,13 +82,14 @@ AUTO_COMMAND="f bar -r" AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_START=11080 + AUTO_NOTIFY_EXPIRE_TIME=15000 run _auto_notify_send assert $state equals 0 assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed' assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds" assert "$lines[3]" same_as "Exit code: 0" - assert "$lines[4]" same_as "--urgency=normal --expire-time=8000" + assert "$lines[4]" same_as "--urgency=normal --expire-time=15000" } @test 'auto-notify-send sends notification on MacOSX' {