From cbea4aeb586cecbd5574910dde445f4e326a7d1a Mon Sep 17 00:00:00 2001 From: Tommy Andersson Date: Mon, 10 Feb 2025 23:45:21 +0100 Subject: [PATCH 1/4] Add option to enable forwarding of notifications over SSH Option AUTO_NOTIFY_ENABLE_SSH has been added to enable and disable forwarding of notifications to local client for long running commands issued over SSH on remote servers. --- README.rst | 13 +++++++++++++ auto-notify.plugin.zsh | 27 +++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index a69648d..6318ced 100644 --- a/README.rst +++ b/README.rst @@ -129,6 +129,19 @@ NOTE: This configuration option currently only works for Linux. # Set notification expiry to 10 seconds export AUTO_NOTIFY_EXPIRE_TIME=10000 +**Notification Forwarding Over SSH** + +You can configure whether notifications will be forwarded to SSH client by setting the environment +variable ``AUTO_NOTIFY_ENABLE_SSH`` to enable ("1") or disable ("0"). The default value is set to 0. +NOTE: This configuration option currently only works for Linux. + +:: + + # Enable notification forwarding to SSH client + export AUTO_NOTIFY_ENABLE_SSH=1 + # Disable notification forwarding to SSH client + export AUTO_NOTIFY_ENABLE_SSH=0 + **Ignored Commands** diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 591a47e..be8b1bb 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -6,6 +6,9 @@ export AUTO_NOTIFY_VERSION="0.10.2" # Threshold in seconds for when to automatically show a notification [[ -z "$AUTO_NOTIFY_THRESHOLD" ]] && export AUTO_NOTIFY_THRESHOLD=10 +# Enable or disable notifications for SSH sessions (0 = disabled, 1 = enabled) +[[ -z "$AUTO_NOTIFY_ENABLE_SSH" ]] && + export AUTO_NOTIFY_ENABLE_SSH=0 # List of commands/programs to ignore sending notifications for [[ -z "$AUTO_NOTIFY_IGNORE" ]] && @@ -65,10 +68,22 @@ function _auto_notify_message() { local arguments=("$title" "$body" "--app-name=zsh" "$transient" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME") - if [[ -n "$icon" ]]; then - arguments+=("--icon=$icon") - fi - notify-send ${arguments[@]} + if [[ -n "$icon" ]]; then + arguments+=("--icon=$icon") + fi + + # Check if the script is running over SSH + if [[ -n "${SSH_CLIENT}" || -n "${SSH_CONNECTION}" ]]; then + # Extract the client IP address from environment + local client_ip="${SSH_CLIENT%% *}" + [[ -z "$client_ip" ]] && client_ip="${SSH_CONNECTION%% *}" + + # Forward the notify-send command to the client machine via SSH + ssh "${USER}@${client_ip}" "$(printf '%q ' notify-send "${arguments[@]}")" + else + # If not running over SSH, send notification locally + notify-send "${arguments[@]}" + fi elif [[ "$platform" == "Darwin" ]]; then osascript \ @@ -90,8 +105,8 @@ function _is_auto_notify_ignored() { # Remove leading whitespace target_command="$(echo "$target_command" | sed -e 's/^ *//')" - # If the command is being run over SSH, then ignore it - if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]]; then + # Ignore the command if running over SSH and AUTO_NOTIFY_ENABLE_SSH is disabled + if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]] && [[ "${AUTO_NOTIFY_ENABLE_SSH-1}" == "0" ]]; then print "yes" return fi From 03179360511ab5e44590af4dce1390f45040f2f7 Mon Sep 17 00:00:00 2001 From: Tommy Andersson Date: Wed, 12 Feb 2025 22:38:53 +0100 Subject: [PATCH 2/4] Add option to configure persistence in notification history Option AUTO_NOTIFY_ENABLE_TRANSIENT has been added to enable and disable persistence in notification history. --- README.rst | 13 +++++++++++++ auto-notify.plugin.zsh | 5 ++++- tests/test_auto_notify_send.zunit | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 6318ced..0d293aa 100644 --- a/README.rst +++ b/README.rst @@ -142,6 +142,19 @@ NOTE: This configuration option currently only works for Linux. # Disable notification forwarding to SSH client export AUTO_NOTIFY_ENABLE_SSH=0 +**Notification Persistence in Notification History** + +You can configure whether notifications will persist in notification history by setting the environment +variable ``AUTO_NOTIFY_ENABLE_TRANSIENT`` to enable ("1") or disable ("0"). The default value is set to 1. +NOTE: This configuration option currently only works for Linux. + +:: + + # Enable persistent notifications + export AUTO_NOTIFY_ENABLE_TRANSIENT=0 + # Disable persistent notifications + export AUTO_NOTIFY_ENABLE_TRANSIENT=1 + **Ignored Commands** diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index be8b1bb..835fd53 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -9,6 +9,9 @@ export AUTO_NOTIFY_VERSION="0.10.2" # Enable or disable notifications for SSH sessions (0 = disabled, 1 = enabled) [[ -z "$AUTO_NOTIFY_ENABLE_SSH" ]] && export AUTO_NOTIFY_ENABLE_SSH=0 +# Enable transient notifications to prevent them from being saved in the notification history +[[ -z "$AUTO_NOTIFY_ENABLE_TRANSIENT" ]] && + export AUTO_NOTIFY_ENABLE_TRANSIENT=1 # List of commands/programs to ignore sending notifications for [[ -z "$AUTO_NOTIFY_IGNORE" ]] && @@ -55,7 +58,7 @@ function _auto_notify_message() { if [[ "$platform" == "Linux" ]]; then local urgency="normal" - local transient="--hint=int:transient:1" + local transient="--hint=int:transient:$AUTO_NOTIFY_ENABLE_TRANSIENT" local icon=${AUTO_NOTIFY_ICON_SUCCESS:-""} # Exit code 130 is returned when a process is terminated with SIGINT. # Since the user is already interacting with the program, there is no diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 49e477f..78123d7 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -89,7 +89,7 @@ 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 "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000" + assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:0 --urgency=normal --expire-time=15000" } @test 'auto-notify-send sends notification and icon on Linux on success' { @@ -104,7 +104,7 @@ 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 "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000 --icon=/path/to/success/icon.png" + assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:0 --urgency=normal --expire-time=15000 --icon=/path/to/success/icon.png" } @test 'auto-notify-send sends notification on macOS' { From 2b1fb4457e2345c035a376e8b7727fcda63de2d4 Mon Sep 17 00:00:00 2001 From: Tommy Andersson Date: Thu, 13 Feb 2025 16:52:07 +0100 Subject: [PATCH 3/4] Add option to cancel notifications on SIGINT Option AUTO_NOTIFY_CANCEL_ON_SIGINT has been added to enable and disable cancellation of notifications when procees is terminated with SIGINT. --- README.rst | 13 +++++++++++++ auto-notify.plugin.zsh | 27 ++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 0d293aa..4d4e54e 100644 --- a/README.rst +++ b/README.rst @@ -155,6 +155,19 @@ NOTE: This configuration option currently only works for Linux. # Disable persistent notifications export AUTO_NOTIFY_ENABLE_TRANSIENT=1 +**Notification Cancel on SIGINT** + +You can configure whether notifications will be cancelled when procees is terminated with SIGINT by setting the environment +variable ``AUTO_NOTIFY_CANCEL_ON_SIGINT`` to enable ("1") or disable ("0"). The default value is set to 0. +NOTE: This configuration option currently only works for Linux. + +:: + + # Enable cancellation of notifications on SIGINT + export AUTO_NOTIFY_CANCEL_ON_SIGINT=1 + # Disable cancellation of notifications on SIGINT + export AUTO_NOTIFY_CANCEL_ON_SIGINT=0 + **Ignored Commands** diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 835fd53..6b00883 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -12,6 +12,10 @@ export AUTO_NOTIFY_VERSION="0.10.2" # Enable transient notifications to prevent them from being saved in the notification history [[ -z "$AUTO_NOTIFY_ENABLE_TRANSIENT" ]] && export AUTO_NOTIFY_ENABLE_TRANSIENT=1 +# Configure whether notifications should be canceled when receiving a SIGINT (Ctrl+C) +[[ -z "$AUTO_NOTIFY_CANCEL_ON_SIGINT" ]] && + export AUTO_NOTIFY_CANCEL_ON_SIGINT=0 + # List of commands/programs to ignore sending notifications for [[ -z "$AUTO_NOTIFY_IGNORE" ]] && @@ -57,16 +61,25 @@ function _auto_notify_message() { body="$(_auto_notify_format "$text" "$command" "$elapsed" "$exit_code")" if [[ "$platform" == "Linux" ]]; then + # Set default notification properties local urgency="normal" local transient="--hint=int:transient:$AUTO_NOTIFY_ENABLE_TRANSIENT" - local icon=${AUTO_NOTIFY_ICON_SUCCESS:-""} - # Exit code 130 is returned when a process is terminated with SIGINT. - # Since the user is already interacting with the program, there is no - # need to make the notification persistent. - if [[ "$exit_code" != "0" ]] && [[ "$exit_code" != "130" ]]; then + local icon="${AUTO_NOTIFY_ICON_SUCCESS:-""}" + + # Handle specific exit codes + if [[ "$exit_code" -eq 130 ]]; then + # Exit code 130 indicates termination by SIGINT (Ctrl+C). + # If AUTO_NOTIFY_CANCEL_ON_SIGINT is enabled, suppress the notification. + if [[ "${AUTO_NOTIFY_CANCEL_ON_SIGINT}" -eq 1 ]]; then + return + fi urgency="critical" - transient="" - icon=${AUTO_NOTIFY_ICON_FAILURE:-""} + transient="--hint=int:transient:1" + icon="${AUTO_NOTIFY_ICON_FAILURE:-""}" + elif [[ "$exit_code" -ne 0 ]]; then + # For all other non-zero exit codes, mark the notification as critical. + urgency="critical" + icon="${AUTO_NOTIFY_ICON_FAILURE:-""}" fi local arguments=("$title" "$body" "--app-name=zsh" "$transient" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME") From 85b12fa87860f064e163f95ab6744ef63c94e440 Mon Sep 17 00:00:00 2001 From: Michael Aquilina Date: Tue, 22 Apr 2025 12:51:48 +0100 Subject: [PATCH 4/4] Explicitly state AUTO_NOTIFY_ENABLE_TRANSIENT state in tests --- tests/test_auto_notify_send.zunit | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 78123d7..d97dd28 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -83,6 +83,7 @@ AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_START=11080 AUTO_NOTIFY_EXPIRE_TIME=15000 + AUTO_NOTIFY_ENABLE_TRANSIENT=0 run _auto_notify_send assert $state equals 0 @@ -97,6 +98,7 @@ AUTO_COMMAND_FULL="foo bar -r" AUTO_COMMAND_START=11080 AUTO_NOTIFY_EXPIRE_TIME=15000 + AUTO_NOTIFY_ENABLE_TRANSIENT=0 AUTO_NOTIFY_ICON_SUCCESS=/path/to/success/icon.png run _auto_notify_send