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/countdown.sh | |
parent | a6b213ce349f91cbe688e3749c0cfa8cadb4a8fa (diff) |
Update
Diffstat (limited to 'bin/unix/countdown.sh')
-rwxr-xr-x | bin/unix/countdown.sh | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh index 683949e..ca6b5d6 100755 --- a/bin/unix/countdown.sh +++ b/bin/unix/countdown.sh | |||
@@ -1,19 +1,34 @@ | |||
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")" |
10 | 22 | ||
11 | |||
12 | start=$(date +%s -d "$input") | 23 | start=$(date +%s -d "$input") |
13 | while [ "$start" -ge $(date +%s) ]; do | 24 | while [ "$start" -ge $(date +%s) ]; do |
14 | ## Is this more than 24h away? | 25 | ## Is this more than 24h away? |
15 | time="$(( $start - `date +%s` ))" | 26 | time="$(( $start - `date +%s` ))" |
16 | printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" | 27 | if [ -z "$SHOWGIVEN" ]; then |
28 | printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" | ||
29 | else | ||
30 | printf '%s\r' "$(date -d "00 + $input" +%H:%M:%S)" | ||
31 | fi | ||
17 | sleep 0.3 | 32 | sleep 0.3 |
18 | done | 33 | done |
19 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | 34 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" |