2014-11-07 02:34:26 +08:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
## Setup
|
2014-11-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08: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-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
zmodload zsh/datetime # faster than `date`
|
2014-11-07 02:34:26 +08:00
|
|
|
|
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
## Zsh Hooks
|
2014-11-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08: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-07 02:34:26 +08:00
|
|
|
}
|
2022-10-15 00:36:48 +08: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-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08: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"
|
|
|
|
|
|
|
|
if [[ $1 -eq 0 ]]; then
|
|
|
|
bgnotify "#win (took $elapsed)" "$2"
|
|
|
|
else
|
|
|
|
bgnotify "#fail (took $elapsed)" "$2"
|
|
|
|
fi
|
2021-11-17 18:44:04 +08:00
|
|
|
}
|
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
# for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116)
|
|
|
|
function bgnotify_appid {
|
2022-10-15 00:07:18 +08:00
|
|
|
if (( ${+commands[osascript]} )); then
|
|
|
|
osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null
|
2022-10-15 00:36:48 +08:00
|
|
|
elif (( ${+commands[xprop]} )); then
|
|
|
|
xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
|
2014-11-08 02:57:02 +08:00
|
|
|
else
|
2022-10-15 00:07:18 +08:00
|
|
|
echo $EPOCHSECONDS
|
2014-11-07 02:34:26 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
function bgnotify {
|
|
|
|
# $1: title, $2: message
|
|
|
|
if (( ${+commands[terminal-notifier]} )); then # macOS
|
2022-10-15 00:07:18 +08:00
|
|
|
local term_id="${bgnotify_termid%%,*}" # remove window id
|
2021-11-17 18:44:04 +08: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
|
|
|
|
|
|
|
|
if [[ -z "$term_id" ]]; then
|
2022-10-15 00:36:48 +08:00
|
|
|
terminal-notifier -message "$2" -title "$1" &>/dev/null
|
2021-11-17 18:44:04 +08:00
|
|
|
else
|
2022-10-15 00:36:48 +08:00
|
|
|
terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" &>/dev/null
|
2021-11-17 18:44:04 +08:00
|
|
|
fi
|
2022-10-15 00:36:48 +08:00
|
|
|
elif (( ${+commands[growlnotify]} )); then # macOS growl
|
2015-06-09 12:35:38 +08:00
|
|
|
growlnotify -m "$1" "$2"
|
2022-10-15 00:36:48 +08:00
|
|
|
elif (( ${+commands[notify-send]} )); then # GNOME
|
2015-06-09 12:35:38 +08:00
|
|
|
notify-send "$1" "$2"
|
2022-10-15 00:36:48 +08:00
|
|
|
elif (( ${+commands[kdialog]} )); then # KDE
|
2018-09-25 19:56:29 +08:00
|
|
|
kdialog --title "$1" --passivepopup "$2" 5
|
2022-10-15 00:36:48 +08:00
|
|
|
elif (( ${+commands[notifu]} )); then # cygwin
|
2014-11-07 02:32:29 +08:00
|
|
|
notifu /m "$2" /p "$1"
|
2014-11-07 02:34:26 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
## Defaults
|
2014-11-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
# notify if command took longer than 5s by default
|
|
|
|
bgnotify_threshold=${bgnotify_threshold:-5}
|
2014-11-07 02:34:26 +08:00
|
|
|
|
2022-10-15 00:36:48 +08:00
|
|
|
# bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
|
|
|
|
bgnotify_termid="$(bgnotify_appid)"
|