|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## setup ## |
| 4 | + |
| 5 | +[[ $- != *i* ]] && return # interactive only! |
| 6 | + |
| 7 | +(( ${bgnotify_threshold} )) || bgnotify_threshold=3 #default 3 seconds |
| 8 | + |
| 9 | + |
| 10 | +## definitions ## |
| 11 | + |
| 12 | +if ! type -t bgnotify_formatted 1>/dev/null; then ## allow custom function override |
| 13 | + function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds) |
| 14 | + elapsed="$(( $3 % 60 ))s" |
| 15 | + (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed" |
| 16 | + (( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed" |
| 17 | + [ $1 -eq 0 ] && bgnotify "#win (took $elapsed)" "$2" || bgnotify "#fail (took $elapsed)" "$2" |
| 18 | + } |
| 19 | +fi |
| 20 | + |
| 21 | +get_timestamp() { |
| 22 | + date +%s |
| 23 | +} |
| 24 | + |
| 25 | +currentWindowId () { |
| 26 | + if hash osascript 2>/dev/null; then #osx |
| 27 | + osascript -e 'tell application (path to frontmost application as text) to id of front window' 2> /dev/null || echo "0" |
| 28 | + elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu! |
| 29 | + xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0" |
| 30 | + else |
| 31 | + echo $(get_timestamp) # fallback for windows |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +bgnotify () { ## args: (title, subtitle) |
| 36 | + tput bel # ring the bell |
| 37 | + |
| 38 | + if hash terminal-notifier 2>/dev/null; then #osx |
| 39 | + [[ "$TERM_PROGRAM" == 'iTerm.app' ]] && term_id='com.googlecode.iterm2'; |
| 40 | + [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] && term_id='com.apple.terminal'; |
| 41 | + ## now call terminal-notifier, (hopefully with $term_id!) |
| 42 | + [ -z "$term_id" ] && terminal-notifier -message "$2" -title "$1" >/dev/null || |
| 43 | + terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null |
| 44 | + elif hash growlnotify 2>/dev/null; then #osx growl |
| 45 | + growlnotify -m "$1" "$2" |
| 46 | + elif hash notify-send 2>/dev/null; then #ubuntu gnome! |
| 47 | + notify-send "$1" "$2" |
| 48 | + elif hash kdialog 2>/dev/null; then #ubuntu kde! |
| 49 | + kdialog -title "$1" --passivepopup "$2" 5 |
| 50 | + elif hash notifu 2>/dev/null; then #cygwyn support! |
| 51 | + notifu /m "$2" /p "$1" |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +## Hooks ## |
| 57 | + |
| 58 | +bgnotify_begin() { |
| 59 | + bgnotify_timestamp=$(get_timestamp) |
| 60 | + bgnotify_lastcmd="$1" |
| 61 | + bgnotify_windowid=$(currentWindowId) |
| 62 | +} |
| 63 | + |
| 64 | +bgnotify_end() { |
| 65 | + didexit=$? |
| 66 | + elapsed=$(( $(get_timestamp) - bgnotify_timestamp )) |
| 67 | + past_threshold=$(( elapsed >= bgnotify_threshold )) |
| 68 | + if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then |
| 69 | + if [ $(currentWindowId) != "$bgnotify_windowid" ]; then |
| 70 | + bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed" |
| 71 | + fi |
| 72 | + fi |
| 73 | + bgnotify_timestamp=0 #reset it to 0! |
| 74 | +} |
| 75 | + |
| 76 | +## only enable if a local (non-ssh) connection |
| 77 | +if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then |
| 78 | + preexec_functions+=(bgnotify_begin) |
| 79 | + precmd_functions+=(bgnotify_end) |
| 80 | +fi |
0 commit comments