diff options
Diffstat (limited to 'bin/unix')
-rwxr-xr-x | bin/unix/countdown.sh | 19 | ||||
-rwxr-xr-x | bin/unix/stopwatch.sh | 19 |
2 files changed, 35 insertions, 3 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)" |
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)" |