chiark / gitweb /
bin/wakey.zsh: Add a new hack to tell me about long-running commands.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 20 Apr 2020 00:22:44 +0000 (01:22 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 20 Apr 2020 00:22:44 +0000 (01:22 +0100)
This idea was from Jonathan Lange's `undistract-me': see

https://github.com/jml/undistract-me

for the original for Bash.

Makefile
bin/wakey.zsh [new file with mode: 0644]

index 1d32b62306a41b898b781f594f314ea0566c1307..ecc3bb8fc3844b9fa461d1731f9e6f6cca6e2cbd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -92,7 +92,7 @@ ELISP                 += dot-emacs
 dot-emacs_DEPS          = make-regexp
 
 SCRIPTLINKS            += emacsclient-hack movemail-hack sendmail-hack
-SCRIPTLINKS            += aspell-hack emerge-hack
+SCRIPTLINKS            += aspell-hack emerge-hack wakey.zsh
 
 %.elc: %.el $$(foreach e, $$($$*_DEPS), $$(DEP_$$e))
        $(call v_tag,EMACS)if ! $(EMACS) >$*.build-log 2>&1 \
diff --git a/bin/wakey.zsh b/bin/wakey.zsh
new file mode 100644 (file)
index 0000000..c5471f8
--- /dev/null
@@ -0,0 +1,40 @@
+### -*-sh-*-
+
+## This idea shamelessly stolen from Jonathan Lange's `undistract-me'; see
+## <https://github.com/jml/undistract-me>.
+
+zmodload zsh/datetime
+
+__wakey_start=nil __wakey_cmd=
+: ${LONG_RUNNING_COMMAND_TIMEOUT=10}; export LONG_RUNNING_COMMAND_TIMEOUT
+
+__wakey_preexec () {
+  case $__wakey_start in
+    nil)
+      __wakey_start=$EPOCHREALTIME __wakey_cmd=$1
+      ;;
+  esac
+}
+
+__wakey_precmd () {
+  typeset icon head rc=$? cmd
+  typeset -F now=$EPOCHREALTIME
+
+  case $__wakey_start in
+    nil) ;;
+    *)
+      if (( now - __wakey_start >= LONG_RUNNING_COMMAND_TIMEOUT )); then
+       case $rc in
+         0) icon=trophy-gold head="Command completed" ;;
+         *) icon=dialog-warning head="Command FAILED (rc = $rc)" ;;
+       esac
+       cmd=${__wakey_cmd//&/&amp;}; cmd=${cmd//</&lt;}; cmd=${cmd//>/&gt;}
+       notify-send -c Wakey -i $icon -t 5000 $head $cmd
+      fi
+      __wakey_start=nil
+      ;;
+  esac
+}
+
+preexec_functions+=(__wakey_preexec)
+precmd_functions+=(__wakey_precmd)