From 2b1fb4457e2345c035a376e8b7727fcda63de2d4 Mon Sep 17 00:00:00 2001 From: Tommy Andersson Date: Thu, 13 Feb 2025 16:52:07 +0100 Subject: [PATCH] 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")