diff options
Diffstat (limited to 'bin/unix/stopwatch.sh')
-rwxr-xr-x | bin/unix/stopwatch.sh | 20 |
1 files changed, 20 insertions, 0 deletions
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 @@ | |||
1 | #! /bin/bash | ||
2 | # Ref: https://superuser.com/questions/611538/611582#611582 | ||
3 | |||
4 | tput civis | ||
5 | read -p '? ' input | ||
6 | |||
7 | [[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" | ||
8 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | ||
9 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | ||
10 | |||
11 | time=0 | ||
12 | start=$(date +%s) | ||
13 | given=$(date +%s -d "$input") | ||
14 | stop=$(( $given - $start )) | ||
15 | while [ $time -ne $stop ]; do | ||
16 | time="$(( $(date +%s) - $start ))" | ||
17 | printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" | ||
18 | sleep 0.3 | ||
19 | done | ||
20 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||