chiark / gitweb /
bin/wakey.zsh: Refactor the logic in `__wakey_precmd'.
[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 suppress=nil
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         suppress=t
32       fi
33       case $suppress in
34         t) ;;
35         *)
36           case $rc in
37             0) icon=trophy-gold head="Command completed" ;;
38             *) icon=dialog-warning head="Command FAILED (rc = $rc)" ;;
39           esac
40           cmd=${__wakey_cmd//&/&amp;}; cmd=${cmd//</&lt;}; cmd=${cmd//>/&gt;}
41           notify-send -c Wakey -i $icon -t 5000 $head $cmd
42       esac
43       __wakey_start=nil
44       ;;
45   esac
46 }
47
48 case ${DISPLAY-nil} in
49   nil) ;;
50   *)
51     if whence notify-send >/dev/null 2>&1; then
52       preexec_functions+=(__wakey_preexec)
53       precmd_functions+=(__wakey_precmd)
54     fi
55     ;;
56 esac