aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/unix/stopwatch.sh
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-02-22 22:17:04 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-02-22 22:17:04 +0800
commite8354fe44d01714d0a1b8a40d1f3d352dd2cb876 (patch)
treeba24ca13ec173491325d0ac96deaaeb60fac3090 /bin/unix/stopwatch.sh
parenta6b213ce349f91cbe688e3749c0cfa8cadb4a8fa (diff)
Update
Diffstat (limited to 'bin/unix/stopwatch.sh')
-rwxr-xr-xbin/unix/stopwatch.sh19
1 files changed, 18 insertions, 1 deletions
diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh
index 70f6b0c..6b76c26 100755
--- a/bin/unix/stopwatch.sh
+++ b/bin/unix/stopwatch.sh
@@ -1,9 +1,21 @@
1#! /bin/bash 1#! /bin/bash
2# Ref: https://superuser.com/questions/611538/611582#611582 2# Ref: https://superuser.com/questions/611538/611582#611582
3 3
4# If user type '^C', then use SIGINT to toggle the display
5trap '[ "$SHOWGIVEN" = yes ] && SHOWGIVEN= || SHOWGIVEN=yes' SIGINT
6# Do not print "^C" when SIGINT caught
7stty -ctlecho
8
9# Hide the cursor
4tput civis 10tput civis
11
12# Wait user input
5read -p '? ' input 13read -p '? ' input
6 14
15# Modify input to fit the format that `date` can read
16# s -> sec
17# m -> min
18# h -> hour
7[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" 19[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")"
8[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" 20[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")"
9[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" 21[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")"
@@ -14,7 +26,12 @@ given=$(date +%s -d "$input")
14stop=$(( $given - $start )) 26stop=$(( $given - $start ))
15while [ $time -ne $stop ]; do 27while [ $time -ne $stop ]; do
16 time="$(( $(date +%s) - $start ))" 28 time="$(( $(date +%s) - $start ))"
17 printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" 29 if [ -z "$SHOWGIVEN" ]; then
30 printf '%s\r' "$(date -u -d @$time +%H:%M:%S)"
31 else
32 printf '%s\r' "$(date -u -d @$stop +%H:%M:%S)"
33 fi
18 sleep 0.3 34 sleep 0.3
19done 35done
36
20printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" 37printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)"