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
|
|
|
|
[[ $bgnotify_timestamp -gt 0 ]] || return
|
|
|
|
[[ $elapsed -ge $bgnotify_threshold ]] || return
|
|
|
|
|
|
|
|
# check if Terminal app is not active
|
|
|
|
[[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return
|
|
|
|
|
|
|
|
printf '\a' # beep sound
|
|
|
|
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-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-11-20 13:54:56 +01:00
|
|
|
# output is "app ID, window ID" (com.googlecode.iterm2, 116)
|
2022-10-14 18:07:18 +02:00
|
|
|
osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
|
2023-11-22 15:22:36 +01:00
|
|
|
elif [[ -n $WAYLAND_DISPLAY ]] && (( ${+commands[swaymsg]} )) && (( ${+commands[jq]} )); then # wayland+sway
|
2023-11-20 13:54:56 +01:00
|
|
|
# output is "app_id, container id" (Alacritty, 1694)
|
|
|
|
swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | {app_id, id} | join(", ")'
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2022-10-14 18:07:18 +02:00
|
|
|
local term_id="${bgnotify_termid%%,*}" # remove window id
|
2021-11-17 11:44:04 +01:00
|
|
|
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
|
|
|
|
|
2023-11-23 11:11:08 +01:00
|
|
|
terminal-notifier -message "$message" -title "$title" ${=icon:+-appIcon "$icon"} ${=term_id:+-activate "$term_id" -sender "$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
|
|
|
|
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)"
|