commit 3a1cd0392a770b9a8fac5049e6b6d8ccd53ec930
parent c4c62232275a981abbf0a5dea3f85a145de68bb5
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 3 May 2023 10:49:12 -0700
Add a set of scripts to facilitate timers and alarms
The beep script uses sox(1) to produce one of a variety of beeping
noises. The alarm script either sends a notification and lets the
beep script run until it is dismissed, or sets itself as an at(1)
command at the time specified. The timer script converts a duration
to an absolute time and passes it to alarm.
The limitations of the alarm and timer script lay in using at(1)
to schedule alarms; it doesn't have second precision.
Diffstat:
3 files changed, 115 insertions(+), 0 deletions(-)
diff --git a/local/bin/bin/alarm b/local/bin/bin/alarm
@@ -0,0 +1,39 @@
+#!/bin/sh
+# Copyright 2023 Jacob R. Edwards <jacob@jacobedwards.org>
+# Set an alarm using at(1)
+#
+# Note that at(1) only works on the minute, so second precision
+# isn't currently supported.
+
+shquote() {
+ sed "s/'/'\\\\''/g
+s/^/'/
+s/$/'/"
+}
+
+while test $# -gt 0
+do
+ case "$1" in
+ (-at) time="$2"; shift ;;
+ (--) shift; break ;;
+ (-*)
+ echo "usage: ${0##*/} [-at time] [notify-args ...]" 1>&2
+ exit 1 ;;
+ (*) break
+ esac
+ shift
+done
+
+if test "$time"
+then
+ echo "DISPLAY='$DISPLAY'" alarm "$(
+ IFS='
+'
+ echo "${*:-Alarm}" | shquote | tr '\n' ' '
+ )" | at -t "$(date -jr "$(timedec "$time")" '+%Y%m%d%H%M.%S')"
+ exit
+fi
+
+sox -q -n -d synth 0.1 sin 880-860 delay 0.8 repeat - &
+trap "kill $!" 0 INT TERM
+notify -s "$@"
diff --git a/local/bin/bin/beep b/local/bin/bin/beep
@@ -0,0 +1,34 @@
+#!/bin/sh
+# Copyright 2023 Jacob R. Edwards <jacob@jacobedwards.org>
+# A script to produce a beeping sound for alerts or the like
+#
+# Has four different urgencies: high, normal, low, and verylow.
+# The default is normal, and it cannot be specified as the urgency
+# argument.
+#
+# Requires sox(1).
+
+d=0.8
+a=880
+b=860
+
+case "$#$1" in
+(0) ;;
+(1high)
+ a=900
+ b=940
+ d=0.1 ;;
+(1low)
+ a=580
+ b=580
+ d=1.5 ;;
+(1verylow)
+ a=580
+ b=580
+ d=4 ;;
+(*)
+ echo "usage ${0:##*/} [urgency]" 1>&2
+ exit 1
+esac
+
+exec sox -q -n -d synth 0.1 sin $a-$b : synth 0.1 sin $a-$b delay $d repeat -
diff --git a/local/bin/bin/timer b/local/bin/bin/timer
@@ -0,0 +1,42 @@
+#!/bin/sh
+# Copyright 2023 Jacob R. Edwards <jacob@jacobedwards.org>
+# Set a timer using the alarm script
+
+set -e
+
+usage() {
+ echo "usage: ${0##*/} [[HH:]MM:]SS [name]" 1>&2
+ exit 1
+}
+
+timedec() {
+ awk -F: \
+'function die(msg) {
+ print(msg) > "/dev/stderr";
+ exit 1
+}
+
+{
+ if (NF > 3)
+ die("Too many fields");
+
+ s = 0;
+ for (i = NF; i > 0; --i) {
+ t = $i;
+ for (j = i; j < NF; ++j)
+ t *= 60;
+ s += t;
+ }
+ print(s);
+}'
+}
+
+case $# in
+([12])
+ seconds="$(echo "$1" | timedec)"
+ shift ;;
+(*)
+ usage ;;
+esac
+
+alarm -at "$(date -jr $(ac $(date +%s)+$seconds) '+%F %T')" "${1:+\"$1\" }timer expired"