bin/wakey.zsh: Refactor the logic in `__wakey_precmd'.
[profile] / bin / wakey.zsh
CommitLineData
9778630e
MW
1### -*-sh-*-
2
3## This idea shamelessly stolen from Jonathan Lange's `undistract-me'; see
4## <https://github.com/jml/undistract-me>.
5
6zmodload zsh/datetime
7
8__wakey_start=nil __wakey_cmd=
9: ${LONG_RUNNING_COMMAND_TIMEOUT=10}; export LONG_RUNNING_COMMAND_TIMEOUT
aeba0b89 10: ${LONG_RUNNING_IGNORE_LIST=}; export LONG_RUNNING_IGNORE_LIST
9778630e
MW
11
12__wakey_preexec () {
13 case $__wakey_start in
14 nil)
aeba0b89
MW
15 case $LONG_RUNNING_IGNORE_LIST in
16 *" $1 "*) ;;
17 *) __wakey_start=$EPOCHREALTIME __wakey_cmd=$1 ;;
18 esac
9778630e
MW
19 ;;
20 esac
21}
22
23__wakey_precmd () {
6131a252 24 typeset icon head rc=$? cmd suppress=nil
9778630e
MW
25 typeset -F now=$EPOCHREALTIME
26
27 case $__wakey_start in
28 nil) ;;
29 *)
6131a252
MW
30 if (( now - __wakey_start < LONG_RUNNING_COMMAND_TIMEOUT )); then
31 suppress=t
9778630e 32 fi
6131a252
MW
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
9778630e
MW
43 __wakey_start=nil
44 ;;
45 esac
46}
47
0b69235c
MW
48case ${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 ;;
56esac