From d0947320bc1b3a1223097749affe798ba225f979 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 22 Feb 2023 15:03:30 +0800 Subject: Update --- bin/unix/stopwatch.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 bin/unix/stopwatch.sh (limited to 'bin/unix/stopwatch.sh') diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh new file mode 100755 index 0000000..70f6b0c --- /dev/null +++ b/bin/unix/stopwatch.sh @@ -0,0 +1,20 @@ +#! /bin/bash +# Ref: https://superuser.com/questions/611538/611582#611582 + +tput civis +read -p '? ' input + +[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" +[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" +[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" + +time=0 +start=$(date +%s) +given=$(date +%s -d "$input") +stop=$(( $given - $start )) +while [ $time -ne $stop ]; do + time="$(( $(date +%s) - $start ))" + printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" + sleep 0.3 +done +printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" -- cgit v1.2.3-70-g09d2