diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-27 16:04:51 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-27 16:04:51 +0800 |
commit | c4377f99fbbb8c95be46b4c6399d1db376688940 (patch) | |
tree | b4bd76f42259fa9a676b294190f7f79d7c2cf03a /bin/unix/stopwatch.sh | |
parent | 005cf5f0fed1d253631e00e228f69d6e3e487771 (diff) |
Update timer.sh
Diffstat (limited to 'bin/unix/stopwatch.sh')
-rwxr-xr-x | bin/unix/stopwatch.sh | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh deleted file mode 100755 index 6b76c26..0000000 --- a/bin/unix/stopwatch.sh +++ /dev/null | |||
@@ -1,37 +0,0 @@ | |||
1 | #! /bin/bash | ||
2 | # Ref: https://superuser.com/questions/611538/611582#611582 | ||
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 | ||
10 | tput civis | ||
11 | |||
12 | # Wait user input | ||
13 | read -p '? ' input | ||
14 | |||
15 | # Modify input to fit the format that `date` can read | ||
16 | # s -> sec | ||
17 | # m -> min | ||
18 | # h -> hour | ||
19 | [[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" | ||
20 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | ||
21 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | ||
22 | |||
23 | time=0 | ||
24 | start=$(date +%s) | ||
25 | given=$(date +%s -d "$input") | ||
26 | stop=$(( $given - $start )) | ||
27 | while [ $time -ne $stop ]; do | ||
28 | time="$(( $(date +%s) - $start ))" | ||
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 | ||
34 | sleep 0.3 | ||
35 | done | ||
36 | |||
37 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||