diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-22 22:17:04 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-22 22:17:04 +0800 |
commit | e8354fe44d01714d0a1b8a40d1f3d352dd2cb876 (patch) | |
tree | ba24ca13ec173491325d0ac96deaaeb60fac3090 /bin/unix/stopwatch.sh | |
parent | a6b213ce349f91cbe688e3749c0cfa8cadb4a8fa (diff) |
Update
Diffstat (limited to 'bin/unix/stopwatch.sh')
-rwxr-xr-x | bin/unix/stopwatch.sh | 19 |
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 | ||
5 | trap '[ "$SHOWGIVEN" = yes ] && SHOWGIVEN= || SHOWGIVEN=yes' SIGINT | ||
6 | # Do not print "^C" when SIGINT caught | ||
7 | stty -ctlecho | ||
8 | |||
9 | # Hide the cursor | ||
4 | tput civis | 10 | tput civis |
11 | |||
12 | # Wait user input | ||
5 | read -p '? ' input | 13 | read -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") | |||
14 | stop=$(( $given - $start )) | 26 | stop=$(( $given - $start )) |
15 | while [ $time -ne $stop ]; do | 27 | while [ $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 |
19 | done | 35 | done |
36 | |||
20 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | 37 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" |