From e6a1db213d66efdaec00469e58d4f9f3f2a78bd0 Mon Sep 17 00:00:00 2001 From: bretello Date: Mon, 20 Nov 2023 13:54:56 +0100 Subject: feat(bgnotify): add support to wayland (#12045) --- plugins/bgnotify/bgnotify.plugin.zsh | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'plugins/bgnotify/bgnotify.plugin.zsh') diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index ed2653aa8..9ad989c0b 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -59,11 +59,14 @@ function bgnotify_formatted { fi } -# for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116) function bgnotify_appid { if (( ${+commands[osascript]} )); then + # output is "app ID, window ID" (com.googlecode.iterm2, 116) osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null - elif (( ${+commands[xprop]} )); then + elif [[ -n $WAYLAND_DISPLAY && ${+commands[swaymsg]} && ${+commands[jq]} ]]; then # wayland+sway + # output is "app_id, container id" (Alacritty, 1694) + swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")' + elif [[ -n $DISPLAY && ${+commands[xprop]} ]]; then xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5 else echo $EPOCHSECONDS @@ -71,7 +74,8 @@ function bgnotify_appid { } function bgnotify { - # $1: title, $2: message + local title="$1" + local message="$2" if (( ${+commands[terminal-notifier]} )); then # macOS local term_id="${bgnotify_termid%%,*}" # remove window id if [[ -z "$term_id" ]]; then @@ -82,18 +86,22 @@ function bgnotify { fi if [[ -z "$term_id" ]]; then - terminal-notifier -message "$2" -title "$1" &>/dev/null + terminal-notifier -message "$message" -title "$title" &>/dev/null else - terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" &>/dev/null + terminal-notifier -message "$message" -title "$title" -activate "$term_id" -sender "$term_id" &>/dev/null fi elif (( ${+commands[growlnotify]} )); then # macOS growl - growlnotify -m "$1" "$2" - elif (( ${+commands[notify-send]} )); then # GNOME - notify-send "$1" "$2" + growlnotify -m "$title" "$message" + elif (( ${+commands[notify-send]} )); then + if [[ -n $ALACRITTY_WINDOW_ID ]]; then + notify-send -i Alacritty "$title" "$message" + else + notify-send "$title" "$message" + fi elif (( ${+commands[kdialog]} )); then # KDE - kdialog --title "$1" --passivepopup "$2" 5 + kdialog --title "$title" --passivepopup "$message" 5 elif (( ${+commands[notifu]} )); then # cygwin - notifu /m "$2" /p "$1" + notifu /m "$message" /p "$title" fi } -- cgit v1.2.3-70-g09d2 From 18073af622703a8aca28c630614d13d2f50d30fe Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Wed, 22 Nov 2023 15:22:36 +0100 Subject: fix(bgnotify): detect properly if `swaymsg` is installed Closes #12053 --- plugins/bgnotify/bgnotify.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/bgnotify/bgnotify.plugin.zsh') diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index 9ad989c0b..493a20815 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -63,10 +63,10 @@ function bgnotify_appid { if (( ${+commands[osascript]} )); then # output is "app ID, window ID" (com.googlecode.iterm2, 116) osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null - elif [[ -n $WAYLAND_DISPLAY && ${+commands[swaymsg]} && ${+commands[jq]} ]]; then # wayland+sway + elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway # output is "app_id, container id" (Alacritty, 1694) swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")' - elif [[ -n $DISPLAY && ${+commands[xprop]} ]]; then + elif [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5 else echo $EPOCHSECONDS -- cgit v1.2.3-70-g09d2 From 38e80e9f21ee981b5f616003c788dcbb9267466c Mon Sep 17 00:00:00 2001 From: Johan Sjöblom Date: Thu, 23 Nov 2023 10:11:08 +0000 Subject: feat(bgnotify): add icon parameter (#12055) --- plugins/bgnotify/bgnotify.plugin.zsh | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'plugins/bgnotify/bgnotify.plugin.zsh') diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index 493a20815..a4a7b621b 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -52,10 +52,10 @@ function bgnotify_formatted { (( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed" (( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed" - if [[ $1 -eq 0 ]]; then - bgnotify "#win (took $elapsed)" "$2" + if [[ $exit_status -eq 0 ]]; then + bgnotify "#win (took $elapsed)" "$cmd" else - bgnotify "#fail (took $elapsed)" "$2" + bgnotify "#fail (took $elapsed)" "$cmd" fi } @@ -66,7 +66,7 @@ function bgnotify_appid { elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway # output is "app_id, container id" (Alacritty, 1694) swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")' - elif [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then + elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5 else echo $EPOCHSECONDS @@ -76,6 +76,7 @@ function bgnotify_appid { function bgnotify { local title="$1" local message="$2" + local icon="$3" if (( ${+commands[terminal-notifier]} )); then # macOS local term_id="${bgnotify_termid%%,*}" # remove window id if [[ -z "$term_id" ]]; then @@ -85,23 +86,15 @@ function bgnotify { esac fi - if [[ -z "$term_id" ]]; then - terminal-notifier -message "$message" -title "$title" &>/dev/null - else - terminal-notifier -message "$message" -title "$title" -activate "$term_id" -sender "$term_id" &>/dev/null - fi + terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null elif (( ${+commands[growlnotify]} )); then # macOS growl growlnotify -m "$title" "$message" elif (( ${+commands[notify-send]} )); then - if [[ -n $ALACRITTY_WINDOW_ID ]]; then - notify-send -i Alacritty "$title" "$message" - else - notify-send "$title" "$message" - fi + notify-send "$title" "$message" ${=icon:+--icon "$icon"} elif (( ${+commands[kdialog]} )); then # KDE kdialog --title "$title" --passivepopup "$message" 5 elif (( ${+commands[notifu]} )); then # cygwin - notifu /m "$message" /p "$title" + notifu /m "$message" /p "$title" ${=icon:+/i "$icon"} fi } -- cgit v1.2.3-70-g09d2 From 9b5b389c22396552086480474c108380817a44a7 Mon Sep 17 00:00:00 2001 From: Johan Sjöblom Date: Sat, 25 Nov 2023 11:27:59 +0100 Subject: refactor(bgnotify): get Sway AppId with `awk` as fallback (#12062) Closes #12062 --- plugins/bgnotify/bgnotify.plugin.zsh | 58 +++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'plugins/bgnotify/bgnotify.plugin.zsh') diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index a4a7b621b..4499d816c 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -63,9 +63,9 @@ function bgnotify_appid { if (( ${+commands[osascript]} )); then # output is "app ID, window ID" (com.googlecode.iterm2, 116) osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null - elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway - # output is "app_id, container id" (Alacritty, 1694) - swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")' + elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )); then # wayland+sway + local app_id=$(find_sway_appid) + [[ -n "$app_id" ]] && echo "$app_id" || echo $EPOCHSECONDS elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5 else @@ -73,19 +73,55 @@ function bgnotify_appid { fi } + +function find_sway_appid { + # output is "app_id,container_id", for example "Alacritty,1694" + # see example swaymsg output: https://github.com/ohmyzsh/ohmyzsh/files/13463939/output.json + if (( ${+commands[jq]} )); then + swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(",")' + else + swaymsg -t get_tree | awk ' + BEGIN { Id = ""; Appid = ""; FocusNesting = -1; Nesting = 0 } + { + # Enter a block + if ($0 ~ /.*{$/) Nesting++ + + # Exit a block. If Nesting is now less than FocusNesting, we have the data we are looking for + if ($0 ~ /^[[:blank:]]*}.*/) { Nesting--; if (FocusNesting > 0 && Nesting < FocusNesting) exit 0 } + + # Save the Id, it is potentially what we are looking for + if ($0 ~ /^[[:blank:]]*"id": [0-9]*,?$/) { sub(/^[[:blank:]]*"id": /, ""); sub(/,$/, ""); Id = $0 } + + # Save the Appid, it is potentially what we are looking for + if ($0 ~ /^[[:blank:]]*"app_id": ".*",?$/) { sub(/^[[:blank:]]*"app_id": "/, ""); sub(/",$/, ""); Appid = $0 } + + # Window is focused, this nesting block contains the Id and Appid we want! + if ($0 ~ /^[[:blank:]]*"focused": true,?$/) { FocusNesting = Nesting } + } + END { + if (Appid != "" && Id != "" && FocusNesting != -1) print Appid "," Id + else print "" + }' + fi +} + +function find_term_id { + local term_id="${bgnotify_termid%%,*}" # remove window id + if [[ -z "$term_id" ]]; then + case "$TERM_PROGRAM" in + iTerm.app) term_id='com.googlecode.iterm2' ;; + Apple_Terminal) term_id='com.apple.terminal' ;; + esac + fi + echo "$term_id" +} + function bgnotify { local title="$1" local message="$2" local icon="$3" if (( ${+commands[terminal-notifier]} )); then # macOS - local term_id="${bgnotify_termid%%,*}" # remove window id - if [[ -z "$term_id" ]]; then - case "$TERM_PROGRAM" in - iTerm.app) term_id='com.googlecode.iterm2' ;; - Apple_Terminal) term_id='com.apple.terminal' ;; - esac - fi - + local term_id=$(find_term_id) terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null elif (( ${+commands[growlnotify]} )); then # macOS growl growlnotify -m "$title" "$message" -- cgit v1.2.3-70-g09d2 From 0a9d82780e20e24b6fafc5b2aaefedb0957986c9 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 1 Dec 2023 17:09:48 +0100 Subject: feat(bgnotify): add option to disable terminal bell (#12077) --- plugins/bgnotify/README.md | 2 ++ plugins/bgnotify/bgnotify.plugin.zsh | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'plugins/bgnotify/bgnotify.plugin.zsh') diff --git a/plugins/bgnotify/README.md b/plugins/bgnotify/README.md index 6bbe3b302..33d529f15 100644 --- a/plugins/bgnotify/README.md +++ b/plugins/bgnotify/README.md @@ -35,12 +35,14 @@ Just add bgnotify to your plugins list in your `.zshrc` One can configure a few things: +- `bgnotify_bell` enabled or disables the terminal bell (default true) - `bgnotify_threshold` sets the notification threshold time (default 6 seconds) - `function bgnotify_formatted` lets you change the notification. You can for instance customize the message and pass in an icon. Use these by adding a function definition before the your call to source. Example: ```sh +bgnotify_bell=false ## disable terminal bell bgnotify_threshold=4 ## set your own notification threshold function bgnotify_formatted { diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index 4499d816c..3c0766191 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -27,7 +27,7 @@ function bgnotify_end { # check if Terminal app is not active [[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return - printf '\a' # beep sound + [[ $bgnotify_bell = true ]] && printf '\a' # beep sound bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed" } always { bgnotify_timestamp=0 @@ -136,6 +136,9 @@ function bgnotify { ## Defaults +# enable terminal bell on notify by default +bgnotify_bell=${bgnotify_bell:-true} + # notify if command took longer than 5s by default bgnotify_threshold=${bgnotify_threshold:-5} -- cgit v1.2.3-70-g09d2