diff options
| author | Marc Cornellà <hello@mcornella.com> | 2022-10-14 18:07:18 +0200 | 
|---|---|---|
| committer | Carlo <carlosalag@protonmail.com> | 2022-11-11 09:53:21 +0100 | 
| commit | 5b2d0a3f06a743cf9d33783276f29ea683db81c3 (patch) | |
| tree | cd1345f199452f197b1a3b309ae46ada54069c18 /plugins/bgnotify | |
| parent | 5bfdd0356b46dc711a8f0866eeb9b717c033d5b2 (diff) | |
| download | zsh-5b2d0a3f06a743cf9d33783276f29ea683db81c3.tar.gz zsh-5b2d0a3f06a743cf9d33783276f29ea683db81c3.tar.bz2 zsh-5b2d0a3f06a743cf9d33783276f29ea683db81c3.zip  | |
perf(bgnotify): cache terminal app ID computation
Fixes #10971
Diffstat (limited to 'plugins/bgnotify')
| -rw-r--r-- | plugins/bgnotify/bgnotify.plugin.zsh | 24 | 
1 files changed, 11 insertions, 13 deletions
diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh index 479796dbe..9c4a62cd7 100644 --- a/plugins/bgnotify/bgnotify.plugin.zsh +++ b/plugins/bgnotify/bgnotify.plugin.zsh @@ -20,25 +20,25 @@ if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom functi    }  fi -currentAppId () { -  if (( $+commands[osascript] )); then -    osascript -e 'tell application (path to frontmost application as text) to id' 2>/dev/null -  fi  } -currentWindowId () { -  if hash osascript 2>/dev/null; then #osx -    osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0" -  elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu! +function currentAppId { +  if (( ${+commands[osascript]} )); then +    # output: 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[notify-send]} || ${+commands[kdialog]} )); then      xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"    else -    echo $EPOCHSECONDS #fallback for windows +    echo $EPOCHSECONDS    fi  } +# currentAppId is expensive (more on macOS!) and it will remain the same until the shell is close +bgnotify_termid=$(currentAppId) +  bgnotify () { ## args: (title, subtitle)    if hash terminal-notifier 2>/dev/null; then #osx -    local term_id="$bgnotify_appid" +    local term_id="${bgnotify_termid%%,*}" # remove window id      if [[ -z "$term_id" ]]; then        case "$TERM_PROGRAM" in        iTerm.app) term_id='com.googlecode.iterm2' ;; @@ -69,8 +69,6 @@ bgnotify () { ## args: (title, subtitle)  bgnotify_begin() {    bgnotify_timestamp=$EPOCHSECONDS    bgnotify_lastcmd="${1:-$2}" -  bgnotify_appid="$(currentAppId)" -  bgnotify_windowid=$(currentWindowId)  }  bgnotify_end() { @@ -78,7 +76,7 @@ bgnotify_end() {    elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))    past_threshold=$(( elapsed >= bgnotify_threshold ))    if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then -    if [[ $(currentAppId) != "$bgnotify_appid" || $(currentWindowId) != "$bgnotify_windowid" ]]; then +    if [[ $(currentAppId) != "$bgnotify_termid" ]]; then        print -n "\a"        bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"      fi  | 
