Skip to content

Commit ba72551

Browse files
committed
Initial commit
0 parents  commit ba72551

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# bash-bgnotify
2+
3+
Cross-platform background notifications for long running commands! Supports OSX and Ubuntu linux.
4+
5+
Forked from Tim O'Brien's awesome [zsh-background-notify](https://github.com/t413/zsh-background-notify) and slightly modified for Bash.
6+
7+
## How to use!
8+
9+
1. Install Bash-Preexec following the instructions on <https://github.com/rcaloras/bash-preexec#install>
10+
2. Clone the repository:
11+
* `git clone https://github.com/zhengpd/bash-bgnotify.git ~/.bash-bgnotify`
12+
3. And add one line your `.bashrc`:
13+
* `source $HOME/.bash-bgnotify/bgnotify.plugin.bash`
14+
4. Done!
15+
16+
## Requirements:
17+
18+
- On OS X you'll need [terminal-notifer](https://github.com/alloy/terminal-notifier)
19+
* `brew install terminal-notifier` (or `gem install terminal-notifier`)
20+
- On ubuntu you're already all set!
21+
- On windows you can use [notifu](http://www.paralint.com/projects/notifu/) or the Cygwin Ports `libnotify` package
22+
23+
## Configuration
24+
25+
One can configure a few things:
26+
27+
- `bgnotify_threshold` sets the notification threshold time (default 6 seconds)
28+
- `function notify_formatted` lets you change the notification
29+
30+
Use these by adding a function definition before the your call to source. Example:
31+
32+
~~~ sh
33+
bgnotify_threshold=4 ## set your own notification threshold
34+
35+
function notify_formatted {
36+
## $1=exit_status, $2=command, $3=elapsed_time
37+
[ $1 -eq 0 ] && title="Holy Smokes Batman!" || title="Holy Graf Zeppelin!"
38+
bgnotify "$title -- after $3 s" "$2";
39+
}
40+
41+
source $HOME/.bash-bgnotify/bgnotify.plugin.bash
42+
~~~
43+
44+
45+
## How it works
46+
47+
[Bash-Preexec](https://github.com/rcaloras/bash-preexec) provides two hook functions for Bash in the style of Zsh: `preexec` that runs before executing a command and `precmd` that runs just before re-prompting. Timing the difference between them gives you execution time!
48+
49+
To check if you're in the background we can use xprop to find the NET_ACTIVE_WINDOW in ubuntu and osascript to run a simple apple script to get the same thing (although slower).

bgnotify.plugin.bash

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)