From 4b457a022c1dda22414bf5d9cacbff525465ed3b Mon Sep 17 00:00:00 2001 From: bretello Date: Fri, 2 Aug 2024 18:19:56 +0200 Subject: [PATCH] zsh: alarm: add elapsed time --- functions/misc.zsh | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/functions/misc.zsh b/functions/misc.zsh index aeb6001..b80f680 100755 --- a/functions/misc.zsh +++ b/functions/misc.zsh @@ -75,22 +75,46 @@ function alarm { echo "Usage: alarm 5[s]|5m|1h" return 1 fi + value=$1 + local t - if [[ $1 = *"m" ]]; then - t=$((${1/m/}*60)) - extra="($1)" - elif [[ $1 = *"h" ]]; then - t=$((${1/h/}*3600)) - extra="($1)" - elif [[ $1 = *"s" ]]; then - t=${1/s/} + if [[ $value = *"m" ]]; then + t=$((${value/m/}*60)) + elif [[ $value = *"h" ]]; then + t=$((${value/h/}*3600)) + elif [[ $value = *"s" ]]; then + t=${value/s/} else - t=$1 + t=$value fi - echo "Setting a timer for $1 $extra" + function format(){ + if [[ $1 -lt 60 ]]; then + printf "00:%02is" $1 + return + fi - sleep $t && beeper + if [[ $1 -lt 3600 ]]; then + minutes=$(($1/60)) + seconds=$(($1%60)) + printf "%02dm:%02dm" $minutes $seconds + return + fi + + hours=$(($1/3600)) + remainder=$(($1%3600)) + printf "%dh:%s" hours "$(format $remainder)" + } + + count=$t + while [[ $count -ge 0 ]]; do + echo -en "Set a timer for $1 │ remaining=$(format $count)" + count=$((count-1)) + sleep 1 + echo -en "\r" + done + echo -e "\nDone!" + beeper }