2014-11-06 19:34:26 +01:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
## Setup
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
[[ -o interactive ]] || return # don't load on non-interactive shells
|
|
|
|
[[ -z "$SSH_CLIENT" && -z "$SSH_TTY" ]] || return # don't load on a SSH connection
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
zmodload zsh/datetime # faster than `date`
|
2014-11-06 19:34:26 +01:00
|
|
|
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
## Zsh Hooks
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
function bgnotify_begin {
|
|
|
|
bgnotify_timestamp=$EPOCHSECONDS
|
|
|
|
bgnotify_lastcmd="${1:-$2}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function bgnotify_end {
|
|
|
|
{
|
|
|
|
local exit_status=$?
|
|
|
|
local elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
|
|
|
|
|
|
|
|
# check time elapsed
|
2023-12-22 09:57:23 +01:00
|
|
|
[[ $bgnotify_timestamp -gt 0 ]] || return 0
|
|
|
|
[[ $elapsed -ge $bgnotify_threshold ]] || return 0
|
2022-10-14 18:36:48 +02:00
|
|
|
|
|
|
|
# check if Terminal app is not active
|
2023-12-22 09:57:23 +01:00
|
|
|
[[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return 0
|
2022-10-14 18:36:48 +02:00
|
|
|
|
|
|
|
bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed"
|
|
|
|
} always {
|
|
|
|
bgnotify_timestamp=0
|
2014-11-06 19:34:26 +01:00
|
|
|
}
|
2022-10-14 18:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
autoload -Uz add-zsh-hook
|
|
|
|
add-zsh-hook preexec bgnotify_begin
|
|
|
|
add-zsh-hook precmd bgnotify_end
|
|
|
|
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
# allow custom function override
|
|
|
|
(( ${+functions[bgnotify_formatted]} )) || \
|
|
|
|
function bgnotify_formatted {
|
|
|
|
local exit_status=$1
|
|
|
|
local cmd="$2"
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
# humanly readable elapsed time
|
|
|
|
local elapsed="$(( $3 % 60 ))s"
|
|
|
|
(( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
|
|
|
|
(( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
|
|
|
|
|
2023-12-27 10:57:53 +01:00
|
|
|
[[ $bgnotify_bell = true ]] && printf '\a' # beep sound
|
2023-11-23 11:11:08 +01:00
|
|
|
if [[ $exit_status -eq 0 ]]; then
|
|
|
|
bgnotify "#win (took $elapsed)" "$cmd"
|
2022-10-14 18:36:48 +02:00
|
|
|
else
|
2023-11-23 11:11:08 +01:00
|
|
|
bgnotify "#fail (took $elapsed)" "$cmd"
|
2022-10-14 18:36:48 +02:00
|
|
|
fi
|
2021-11-17 11:44:04 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
function bgnotify_appid {
|
2022-10-14 18:07:18 +02:00
|
|
|
if (( ${+commands[osascript]} )); then
|
2023-12-27 10:57:53 +01:00
|
|
|
osascript -e "tell application id \"$(bgnotify_programid)\" to get the {id, frontmost, id of front window, visible of front window}" 2>/dev/null
|
2023-11-25 11:27:59 +01:00
|
|
|
elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )); then # wayland+sway
|
2023-12-27 10:57:53 +01:00
|
|
|
local app_id=$(bgnotify_find_sway_appid)
|
2023-11-25 11:27:59 +01:00
|
|
|
[[ -n "$app_id" ]] && echo "$app_id" || echo $EPOCHSECONDS
|
2023-11-23 11:11:08 +01:00
|
|
|
elif [[ -z $WAYLAND_DISPLAY ]] && [[ -n $DISPLAY ]] && (( ${+commands[xprop]} )); then
|
2022-10-14 18:36:48 +02:00
|
|
|
xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
|
2014-11-07 19:57:02 +01:00
|
|
|
else
|
2022-10-14 18:07:18 +02:00
|
|
|
echo $EPOCHSECONDS
|
2014-11-06 19:34:26 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-25 11:27:59 +01:00
|
|
|
|
2023-12-27 10:57:53 +01:00
|
|
|
function bgnotify_find_sway_appid {
|
2023-11-25 11:27:59 +01:00
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2023-12-27 10:57:53 +01:00
|
|
|
function bgnotify_programid {
|
|
|
|
case "$TERM_PROGRAM" in
|
|
|
|
iTerm.app) echo 'com.googlecode.iterm2' ;;
|
|
|
|
Apple_Terminal) echo 'com.apple.terminal' ;;
|
|
|
|
esac
|
2023-11-25 11:27:59 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
function bgnotify {
|
2023-11-20 13:54:56 +01:00
|
|
|
local title="$1"
|
|
|
|
local message="$2"
|
2023-11-23 11:11:08 +01:00
|
|
|
local icon="$3"
|
2022-10-14 18:36:48 +02:00
|
|
|
if (( ${+commands[terminal-notifier]} )); then # macOS
|
2023-12-27 10:57:53 +01:00
|
|
|
local term_id=$(bgnotify_programid)
|
2024-06-04 15:55:08 +02:00
|
|
|
terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id"} &>/dev/null
|
2022-10-14 18:36:48 +02:00
|
|
|
elif (( ${+commands[growlnotify]} )); then # macOS growl
|
2023-11-20 13:54:56 +01:00
|
|
|
growlnotify -m "$title" "$message"
|
|
|
|
elif (( ${+commands[notify-send]} )); then
|
2023-11-23 11:11:08 +01:00
|
|
|
notify-send "$title" "$message" ${=icon:+--icon "$icon"}
|
2022-10-14 18:36:48 +02:00
|
|
|
elif (( ${+commands[kdialog]} )); then # KDE
|
2023-11-20 13:54:56 +01:00
|
|
|
kdialog --title "$title" --passivepopup "$message" 5
|
2022-10-14 18:36:48 +02:00
|
|
|
elif (( ${+commands[notifu]} )); then # cygwin
|
2023-11-23 11:11:08 +01:00
|
|
|
notifu /m "$message" /p "$title" ${=icon:+/i "$icon"}
|
2014-11-06 19:34:26 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
## Defaults
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2023-12-01 17:09:48 +01:00
|
|
|
# enable terminal bell on notify by default
|
|
|
|
bgnotify_bell=${bgnotify_bell:-true}
|
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
# notify if command took longer than 5s by default
|
|
|
|
bgnotify_threshold=${bgnotify_threshold:-5}
|
2014-11-06 19:34:26 +01:00
|
|
|
|
2022-10-14 18:36:48 +02:00
|
|
|
# bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
|
|
|
|
bgnotify_termid="$(bgnotify_appid)"
|