diff options
-rwxr-xr-x | scripts/timer | 65 |
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 |