chiark / gitweb /
94638caa84ebad8518ae2e252fb17671bc1fb99d
[profile] / bin / wakey.zsh
1 ### -*-sh-*-
2
3 ## This idea shamelessly stolen from Jonathan Lange's `undistract-me'; see
4 ## <https://github.com/jml/undistract-me>.
5
6 zmodload zsh/datetime
7
8 __wakey_start=nil __wakey_cmd=
9 : ${LONG_RUNNING_COMMAND_TIMEOUT=10}; export LONG_RUNNING_COMMAND_TIMEOUT
10 : ${LONG_RUNNING_IGNORE_LIST=}; export LONG_RUNNING_IGNORE_LIST
11
12 __wakey_preexec () {
13   case $__wakey_start in
14     nil)
15       case $LONG_RUNNING_IGNORE_LIST in
16         *" $1 "*) ;;
17         *) __wakey_start=$EPOCHREALTIME __wakey_cmd=$1 ;;
18       esac
19       ;;
20   esac
21 }
22
23 __wakey_precmd () {
24   typeset icon head rc=$? cmd
25   typeset -F now=$EPOCHREALTIME
26
27   case $__wakey_start in
28     nil) ;;
29     *)
30       if (( now - __wakey_start >= LONG_RUNNING_COMMAND_TIMEOUT )); then
31         case $rc in
32           0) icon=trophy-gold head="Command completed" ;;
33           *) icon=dialog-warning head="Command FAILED (rc = $rc)" ;;
34         esac
35         cmd=${__wakey_cmd//&/&amp;}; cmd=${cmd//</&lt;}; cmd=${cmd//>/&gt;}
36         notify-send -c Wakey -i $icon -t 5000 $head $cmd
37       fi
38       __wakey_start=nil
39       ;;
40   esac
41 }
42
43 case ${DISPLAY-nil} in
44   nil) ;;
45   *)
46     if whence notify-send >/dev/null 2>&1; then
47       preexec_functions+=(__wakey_preexec)
48       precmd_functions+=(__wakey_precmd)
49     fi
50     ;;
51 esac