aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVG <vg@devys.org>2017-08-20 21:10:54 +0200
committerVG <vg@devys.org>2017-08-20 21:10:54 +0200
commit6ccc66d81bafb209063441023717b4a1f28f0162 (patch)
treeee645f337bd6e9941c2c73debf8649ad8088ab27
parentb981356bc365eb1857696926842f306a6d30b5a2 (diff)
downloadscripts-6ccc66d81bafb209063441023717b4a1f28f0162.tar.gz
scripts-6ccc66d81bafb209063441023717b4a1f28f0162.tar.bz2
scripts-6ccc66d81bafb209063441023717b4a1f28f0162.zip
add a timer script
-rwxr-xr-xscripts/timer65
1 files changed, 65 insertions, 0 deletions
diff --git a/scripts/timer b/scripts/timer
new file mode 100755
index 0000000..04522d6
--- /dev/null
+++ b/scripts/timer
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+############################################################
+# Argument parsing
+############################################################
+
+# We need TEMP as the 'eval set --' would nuke the return value of getopt.
+TEMP=$(getopt -o 'm:hr:' --long 'message:,help,repetition:' -n 'timer' -- "$@")
+
+if [ $? -ne 0 ]; then
+ echo 'Terminating...' >&2
+ exit 1
+fi
+
+# Note the quotes around "$TEMP": they are essential!
+eval set -- "$TEMP"
+unset TEMP
+
+message=''
+repetition='60'
+while :; do
+ case "$1" in
+ '-m'|'--message') message="$2"; shift 2; continue;;
+ '-r'|'--repetition') repetition="$2"; shift 2; continue;;
+ '-h'|'--help') cat <<EOF
+timer [OPTIONS]... TIME
+
+Options:
+
+ -h, --help show this help screen (TIME optional in this case)
+ -m M, --message M Use M as message to show and say
+ -r S, --repetition S Wait S seconds between each repetition
+EOF
+ exit 0
+ ;;
+ '--') shift; break;;
+ *) echo 'Internal error !' >&2; exit 1 ;;
+ esac
+done
+
+
+time="$1"
+shift
+
+if [[ "$message" = "" ]]; then message="Timer \"$time\" is over."; fi
+
+############################################################
+# Main code
+############################################################
+
+sleep=sleep
+if type -p fancy-sleep >/dev/null; then sleep=fancy-sleep; fi
+
+"$sleep" "$time" || exit 0
+
+while :; do
+ printf "\a"
+ if test "$#" -gt 0; then
+ "$@"
+ else
+ notify-send "$message"
+ spd-say-en "$message"
+ fi
+ "$sleep" "$repetition" || break
+done