commit 4b06abd7ce571bed117353194e0fd2f2885c7e9c from: Benjamin Stürz date: Sun Aug 04 13:11:41 2024 UTC bedstatus/timer: add more flags commit - 9edd225effe94b501a9283dba6bee3ed13d76663 commit + 4b06abd7ce571bed117353194e0fd2f2885c7e9c blob - 3cee31b77f6b04aca658fb3c0a7f7706ede06435 blob + 42a722b89ba594f516bdae63dcdd22c4e68d53cb --- bedstatus/timer +++ bedstatus/timer @@ -3,7 +3,7 @@ timerfile="${XDG_RUNTIME_DIR}/bedstatus-timer" usage() { - echo "Usage: timer [-r|-s duration]" + echo "Usage: timer [-r|-s seconds|-m minutes|-h hours]" exit 1 } @@ -11,30 +11,37 @@ update() { pkill -USR1 bedstatus } -set_timer() { - dur=$1 - now=$(date +%s) - timer=$((now + dur)) - echo "$timer" > "$timerfile" - update -} - -args=$(getopt 'hs:r' $*) +args=$(getopt 'as:m:h:r' $*) [ $? -ne 0 ] && usage set -- $args +add=0 +duration=0 + while true; do case "$1" in + -a) + add=1 + shift + ;; -r) rm -f "$timerfile" update exit ;; -s) - set_timer "$2" - exit + duration=$((duration + $2)) + shift 2 ;; + -m) + duration=$((duration + $2 * 60)) + shift 2 + ;; + -h) + duration=$((duration + $2 * 60 * 60)) + shift 2 + ;; --) shift break @@ -46,4 +53,18 @@ while true; do esac done -cat "$timerfile" +if [ "$duration" -ne 0 ]; then + if [ "$add" = 1 ]; then + old=$(cat "$timerfile") + timer=$((old + duration)) + else + now=$(date +%s) + timer=$((now + duration)) + fi + echo "$timer" > "$timerfile" + update +elif [ $# -eq 0 ]; then + cat "$timerfile" +else + usage +fi