]> gitweb.fluxo.info Git - utils-x11.git/commitdiff
Enhanced xalarm
authorSilvio Rhatto <rhatto@riseup.net>
Wed, 14 Mar 2018 12:43:40 +0000 (09:43 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Wed, 14 Mar 2018 12:43:40 +0000 (09:43 -0300)
xalarm

diff --git a/xalarm b/xalarm
index 712ee6510adc69846495ade31e5cd135d618dae4..c16132c72064bb7940141a381771b8e2609e5b83 100755 (executable)
--- a/xalarm
+++ b/xalarm
@@ -4,18 +4,61 @@
 # See discussion at https://www.reddit.com/r/Gentoo/comments/1rryh1/kalarm_replacement/
 #
 
-# Delay
-DELAY="${1:-10m}"
+# Parameters
+FULLNAME="$0"
+BASENAME="`basename $0`"
 
-# Message
-shift
-MESSAGE="${*:-Alarm!}"
+# List alarms
+function xalarm_list {
+  ps -U $USER -o pid,command | grep xalarm | grep -v grep | grep -v list | grep -v cancel | \
+    sed -e "s|$FULLNAME||" -e "s|/bin/bash||" | grep -v -- "sed -e"
+}
 
-# AT(1) implementation
-# Example run: xalarm 1minute mymessage
-#echo "xmessage $MESSAGE" | at now +$DELAY
+# Set alarm
+function xalarm_set {
+  # Delay
+  DELAY="${1:-10m}"
 
-# Sleep implementation
-# Example run: xalarm 1m mymessage
-sleep $DELAY
-xmessage $MESSAGE
+  # Message
+  shift
+  MESSAGE="${*:-Alarm!}"
+
+  # AT(1) implementation
+  # Example run: xalarm 1minute mymessage
+  #echo "xmessage $MESSAGE" | at now +$DELAY
+
+  # Sleep implementation
+  # Example run: xalarm 1m mymessage
+  sleep $DELAY
+  xmessage $MESSAGE
+}
+
+# Cancel alarms
+function xalarm_cancel {
+  if [ ! -z "$1" ]; then
+    if xalarm_list | awk '{ print $1 }' | grep -q "^$1"; then
+      kill $1
+    fi
+  else
+    for pid in `xalarm_list | awk '{ print $1 }'`; do
+      kill $pid
+    done
+  fi
+}
+
+# Usage
+function xalarm_usage {
+  echo "usage: $BASENAME [list|cancel|kill|help|usage] [timedef] [message]"
+  exit 1
+}
+
+# Dispatch
+if [ "$1" == "list" ]; then
+  xalarm_list
+elif [ "$1" == "cancel" ] || [ "$1" == "kill" ]; then
+  xalarm_cancel $2
+elif [ "$1" == "help" ] || [ "$1" == "usage" ]; then
+  xalarm_usage
+else
+  xalarm_set $*
+fi