From 4968399e4a767534eb1c981c44c051b8ff7112ef Mon Sep 17 00:00:00 2001 From: tiyn Date: Thu, 26 Oct 2023 04:08:55 +0200 Subject: [PATCH] tools: added timer script --- .local/bin/tools/timer | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 .local/bin/tools/timer diff --git a/.local/bin/tools/timer b/.local/bin/tools/timer new file mode 100755 index 0000000..1323ca2 --- /dev/null +++ b/.local/bin/tools/timer @@ -0,0 +1,36 @@ +#!/bin/dash + +help=""" +usage: timer [option] [args] + options: + -c SECONDS Count down from given amount of seconds + -s Start stopwatch + -h Display this message +""" + + +countdown() { + start="$(( $(date '+%s') + $1))" + while [ $start -ge $(date +%s) ]; do + time="$(( $start - $(date +%s) ))" + printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" + sleep 0.1 + done +} + +stopwatch() { + start=$(date +%s) + while true; do + time="$(( $(date +%s) - $start))" + printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" + sleep 0.1 + done +} + +while getopts "c:sh" arg; do + case $arg in + c) countdown $OPTARG;; + s) stopwatch;; + h) echo "$help";; + esac +done