diff --git a/README.rst b/README.rst index a69648d..4d4e54e 100644 --- a/README.rst +++ b/README.rst @@ -129,6 +129,45 @@ 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 + +**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 + +**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 591a47e..6b00883 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -6,6 +6,16 @@ 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 +# 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" ]] && @@ -51,24 +61,45 @@ 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:1" - 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 transient="--hint=int:transient:$AUTO_NOTIFY_ENABLE_TRANSIENT" + 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") - 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 +121,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 diff --git a/tests/test_auto_notify_send.zunit b/tests/test_auto_notify_send.zunit index 49e477f..d97dd28 100644 --- a/tests/test_auto_notify_send.zunit +++ b/tests/test_auto_notify_send.zunit @@ -83,13 +83,14 @@ 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 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' { @@ -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 @@ -104,7 +106,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' {