summaryrefslogtreecommitdiff
path: root/plugins/bgnotify
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2023-12-09 13:20:13 -0800
committerTuowen Zhao <ztuowen@gmail.com>2023-12-09 13:20:13 -0800
commit7e951c254e779ff0620537cf43ca69dd878387b4 (patch)
treecb042e695bb3e11ed0483fad1af8a5b4f1bfc8d8 /plugins/bgnotify
parent4d908094fdc2a0c0e9a0a072eba213fab7adef43 (diff)
parent48ccc7b36de8efb2bd7beb9bd6e0a6f6fe03b95d (diff)
downloadzsh-7e951c254e779ff0620537cf43ca69dd878387b4.tar.gz
zsh-7e951c254e779ff0620537cf43ca69dd878387b4.tar.bz2
zsh-7e951c254e779ff0620537cf43ca69dd878387b4.zip
Merge remote-tracking branch 'github/master'
Diffstat (limited to 'plugins/bgnotify')
-rw-r--r--plugins/bgnotify/README.md29
-rw-r--r--plugins/bgnotify/bgnotify.plugin.zsh88
2 files changed, 83 insertions, 34 deletions
diff --git a/plugins/bgnotify/README.md b/plugins/bgnotify/README.md
index 1d8fac54d..33d529f15 100644
--- a/plugins/bgnotify/README.md
+++ b/plugins/bgnotify/README.md
@@ -1,19 +1,19 @@
# bgnotify zsh plugin
-cross-platform background notifications for long running commands! Supports OSX and Ubuntu linux.
+cross-platform background notifications for long running commands! Supports OSX and Linux.
Standalone homepage: [t413/zsh-background-notify](https://github.com/t413/zsh-background-notify)
-----------------------------------
+---
-## How to use!
+## How to use
Just add bgnotify to your plugins list in your `.zshrc`
- On OS X you'll need [terminal-notifier](https://github.com/alloy/terminal-notifier)
* `brew install terminal-notifier` (or `gem install terminal-notifier`)
-- On ubuntu you're already all set!
-- On windows you can use [notifu](https://www.paralint.com/projects/notifu/) or the Cygwin Ports libnotify package
+- On Linux, make sure you have `notify-send` or `kdialog` installed. If you're using Ubuntu you should already be all set!
+- On Windows you can use [notifu](https://www.paralint.com/projects/notifu/) or the Cygwin Ports libnotify package
## Screenshots
@@ -35,20 +35,29 @@ 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
+- `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
+```sh
+bgnotify_bell=false ## disable terminal bell
bgnotify_threshold=4 ## set your own notification threshold
function bgnotify_formatted {
## $1=exit_status, $2=command, $3=elapsed_time
- [ $1 -eq 0 ] && title="Holy Smokes Batman!" || title="Holy Graf Zeppelin!"
- bgnotify "$title -- after $3 s" "$2";
+
+ # Humanly readable elapsed time
+ local elapsed="$(( $3 % 60 ))s"
+ (( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
+ (( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
+
+ [ $1 -eq 0 ] && title="Holy Smokes Batman" || title="Holy Graf Zeppelin"
+ [ $1 -eq 0 ] && icon="$HOME/icons/success.png" || icon="$HOME/icons/fail.png"
+ bgnotify "$title - took ${elapsed}" "$2" "$icon"
}
plugins=(git bgnotify) ## add to plugins list
source $ZSH/oh-my-zsh.sh ## existing source call
-~~~
+```
diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
index ed2653aa8..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
@@ -52,53 +52,93 @@ 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
}
-# 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]} )); 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
echo $EPOCHSECONDS
fi
}
-function bgnotify {
- # $1: title, $2: message
- if (( ${+commands[terminal-notifier]} )); then # macOS
- local term_id="${bgnotify_termid%%,*}" # remove window id
- if [[ -z "$term_id" ]]; then
- case "$TERM_PROGRAM" in
+
+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
+ esac
+ fi
+ echo "$term_id"
+}
- if [[ -z "$term_id" ]]; then
- terminal-notifier -message "$2" -title "$1" &>/dev/null
- else
- terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" &>/dev/null
- fi
+function bgnotify {
+ local title="$1"
+ local message="$2"
+ local icon="$3"
+ if (( ${+commands[terminal-notifier]} )); then # macOS
+ 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 "$1" "$2"
- elif (( ${+commands[notify-send]} )); then # GNOME
- notify-send "$1" "$2"
+ growlnotify -m "$title" "$message"
+ elif (( ${+commands[notify-send]} )); then
+ notify-send "$title" "$message" ${=icon:+--icon "$icon"}
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" ${=icon:+/i "$icon"}
fi
}
## 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}