diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/unix/countdown.sh | 34 | ||||
-rwxr-xr-x | bin/unix/stopwatch.sh | 37 | ||||
-rwxr-xr-x | bin/unix/timer.sh | 62 |
3 files changed, 62 insertions, 71 deletions
diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh deleted file mode 100755 index ca6b5d6..0000000 --- a/bin/unix/countdown.sh +++ /dev/null | |||
@@ -1,34 +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 | start=$(date +%s -d "$input") | ||
24 | while [ "$start" -ge $(date +%s) ]; do | ||
25 | ## Is this more than 24h away? | ||
26 | time="$(( $start - `date +%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 | ||
32 | sleep 0.3 | ||
33 | done | ||
34 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||
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)" | ||
diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh new file mode 100755 index 0000000..ac9c858 --- /dev/null +++ b/bin/unix/timer.sh | |||
@@ -0,0 +1,62 @@ | |||
1 | #! /bin/bash | ||
2 | # Ref: https://superuser.com/questions/611538/611582#611582 | ||
3 | |||
4 | SIGNAL=${1:-SIGTERM} | ||
5 | |||
6 | # If SIGNAL is received, switch to next display | ||
7 | trap 'next_display' $SIGNAL | ||
8 | #trap 'toggle_timer' SIGTSTP | ||
9 | # Do not print "^C" when SIGINT caught | ||
10 | stty -ctlecho | ||
11 | |||
12 | # Hide the cursor | ||
13 | tput civis | ||
14 | |||
15 | display_list=(STOPWATCH COUNTDOWN PERIOD) | ||
16 | export DISPLAY=0 | ||
17 | |||
18 | next_display() { | ||
19 | export DISPLAY=$(( ($DISPLAY + 1) %${#display_list[@]} )) | ||
20 | } | ||
21 | |||
22 | export stop= | ||
23 | toggle_timer() { | ||
24 | [ "$stop" = true ] && export stop= || export stop=true | ||
25 | } | ||
26 | |||
27 | # Wait user input | ||
28 | read -p '? ' input | ||
29 | |||
30 | # Modify input to fit the format that `date` can read | ||
31 | # s -> sec | ||
32 | # m -> min | ||
33 | # h -> hour | ||
34 | [[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" | ||
35 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | ||
36 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | ||
37 | |||
38 | start=$(date +%s) # unix epoch of start | ||
39 | given=$(date +%s -d "$input") # unix epoch of end | ||
40 | period=$(( $given - $start )) # seconds for timer | ||
41 | |||
42 | time=0 | ||
43 | while [ $time -ne $period ]; do | ||
44 | [ "$stop" = true ] && sleep 0.3 && continue | ||
45 | |||
46 | time="$(( $(date +%s) - $start ))" | ||
47 | case ${display_list[$DISPLAY]} in | ||
48 | STOPWATCH) | ||
49 | printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" | ||
50 | ;; | ||
51 | COUNTDOWN) | ||
52 | printf '%s\r' "$(date -u -d @$((period - $time)) +%H:%M:%S)" | ||
53 | ;; | ||
54 | PERIOD) | ||
55 | printf '%s\r' "$(date -u -d @$period +%H:%M:%S)" | ||
56 | ;; | ||
57 | esac | ||
58 | |||
59 | sleep 0.3 | ||
60 | done | ||
61 | |||
62 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | ||