9b5b389c22396552086480474c108380817a44a7

Author
Johan Sjöblom <sjoblomj88@gmail.com>
Committer
Marc Cornellà <marc@mcornella.com>
Date

Message

refactor(bgnotify): get Sway AppId with `awk` as fallback (#12062)

Closes #12062

Diff

 1diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
 2index a4a7b621be35bb45929f5c2acca8e7f03c5a012d..4499d816cd4545864724fbb951191196ba21ed62 100644
 3--- a/plugins/bgnotify/bgnotify.plugin.zsh
 4+++ b/plugins/bgnotify/bgnotify.plugin.zsh
 5@@ -63,9 +63,9 @@ function bgnotify_appid {
 6   if (( ${+commands[osascript]} )); then
 7     # output is "app ID, window ID" (com.googlecode.iterm2, 116)
 8     osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
 9-  elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway
10-    # output is "app_id, container id" (Alacritty, 1694)
11-    swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")'
12+  elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )); then # wayland+sway
13+    local app_id=$(find_sway_appid)
14+    [[ -n "$app_id" ]] && echo "$app_id" || echo $EPOCHSECONDS
15   elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then
16     xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
17   else
18@@ -73,19 +73,55 @@ function bgnotify_appid {
19   fi
20 }
21 
22+
23+function find_sway_appid {
24+  # output is "app_id,container_id", for example "Alacritty,1694"
25+  # see example swaymsg output: https://github.com/ohmyzsh/ohmyzsh/files/13463939/output.json
26+  if (( ${+commands[jq]} )); then
27+    swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(",")'
28+  else
29+    swaymsg -t get_tree | awk '
30+      BEGIN { Id = ""; Appid = ""; FocusNesting = -1; Nesting = 0 }
31+      {
32+        # Enter a block
33+        if ($0 ~ /.*{$/) Nesting++
34+
35+        # Exit a block. If Nesting is now less than FocusNesting, we have the data we are looking for
36+        if ($0 ~ /^[[:blank:]]*}.*/) { Nesting--; if (FocusNesting > 0 && Nesting < FocusNesting) exit 0 }
37+
38+        # Save the Id, it is potentially what we are looking for
39+        if ($0 ~ /^[[:blank:]]*"id": [0-9]*,?$/)    { sub(/^[[:blank:]]*"id": /, "");      sub(/,$/,  ""); Id = $0 }
40+
41+        # Save the Appid, it is potentially what we are looking for
42+        if ($0 ~ /^[[:blank:]]*"app_id": ".*",?$/)  { sub(/^[[:blank:]]*"app_id": "/, ""); sub(/",$/, ""); Appid = $0 }
43+
44+        # Window is focused, this nesting block contains the Id and Appid we want!
45+        if ($0 ~ /^[[:blank:]]*"focused": true,?$/) { FocusNesting = Nesting }
46+      }
47+      END {
48+        if (Appid != "" && Id != "" && FocusNesting != -1) print Appid "," Id
49+        else print ""
50+      }'
51+  fi
52+}
53+
54+function find_term_id {
55+  local term_id="${bgnotify_termid%%,*}" # remove window id
56+  if [[ -z "$term_id" ]]; then
57+    case "$TERM_PROGRAM" in
58+      iTerm.app) term_id='com.googlecode.iterm2' ;;
59+      Apple_Terminal) term_id='com.apple.terminal' ;;
60+    esac
61+  fi
62+  echo "$term_id"
63+}
64+
65 function bgnotify {
66   local title="$1"
67   local message="$2"
68   local icon="$3"
69   if (( ${+commands[terminal-notifier]} )); then # macOS
70-    local term_id="${bgnotify_termid%%,*}" # remove window id
71-    if [[ -z "$term_id" ]]; then
72-      case "$TERM_PROGRAM" in
73-      iTerm.app) term_id='com.googlecode.iterm2' ;;
74-      Apple_Terminal) term_id='com.apple.terminal' ;;
75-      esac
76-    fi
77-
78+    local term_id=$(find_term_id)
79     terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$term_id"} &>/dev/null
80   elif (( ${+commands[growlnotify]} )); then # macOS growl
81     growlnotify -m "$title" "$message"