5b2d0a3f06a743cf9d33783276f29ea683db81c3

Author
Marc Cornellà <hello@mcornella.com>
Committer
Carlo <carlosalag@protonmail.com>
Date

Message

perf(bgnotify): cache terminal app ID computation

Fixes #10971

Diff

 1diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
 2index 479796dbec57202492a7f5c7a8e8cf1c161f2f6a..9c4a62cd76f6b3a6329f53f87a7908ae9e205fea 100644
 3--- a/plugins/bgnotify/bgnotify.plugin.zsh
 4+++ b/plugins/bgnotify/bgnotify.plugin.zsh
 5@@ -20,25 +20,25 @@ if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom functi
 6   }
 7 fi
 8 
 9-currentAppId () {
10-  if (( $+commands[osascript] )); then
11-    osascript -e 'tell application (path to frontmost application as text) to id' 2>/dev/null
12-  fi
13 }
14 
15-currentWindowId () {
16-  if hash osascript 2>/dev/null; then #osx
17-    osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
18-  elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu!
19+function currentAppId {
20+  if (( ${+commands[osascript]} )); then
21+    # output: com.googlecode.iterm2, 116
22+    osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
23+  elif (( ${+commands[notify-send]} || ${+commands[kdialog]} )); then
24     xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
25   else
26-    echo $EPOCHSECONDS #fallback for windows
27+    echo $EPOCHSECONDS
28   fi
29 }
30 
31+# currentAppId is expensive (more on macOS!) and it will remain the same until the shell is close
32+bgnotify_termid=$(currentAppId)
33+
34 bgnotify () { ## args: (title, subtitle)
35   if hash terminal-notifier 2>/dev/null; then #osx
36-    local term_id="$bgnotify_appid"
37+    local term_id="${bgnotify_termid%%,*}" # remove window id
38     if [[ -z "$term_id" ]]; then
39       case "$TERM_PROGRAM" in
40       iTerm.app) term_id='com.googlecode.iterm2' ;;
41@@ -69,8 +69,6 @@ bgnotify () { ## args: (title, subtitle)
42 bgnotify_begin() {
43   bgnotify_timestamp=$EPOCHSECONDS
44   bgnotify_lastcmd="${1:-$2}"
45-  bgnotify_appid="$(currentAppId)"
46-  bgnotify_windowid=$(currentWindowId)
47 }
48 
49 bgnotify_end() {
50@@ -78,7 +76,7 @@ bgnotify_end() {
51   elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
52   past_threshold=$(( elapsed >= bgnotify_threshold ))
53   if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
54-    if [[ $(currentAppId) != "$bgnotify_appid" || $(currentWindowId) != "$bgnotify_windowid" ]]; then
55+    if [[ $(currentAppId) != "$bgnotify_termid" ]]; then
56       print -n "\a"
57       bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
58     fi