diff options
-rwxr-xr-x | bin/unix/timer.sh | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh index ac9c858..ea15985 100755 --- a/bin/unix/timer.sh +++ b/bin/unix/timer.sh | |||
@@ -4,8 +4,8 @@ | |||
4 | SIGNAL=${1:-SIGTERM} | 4 | SIGNAL=${1:-SIGTERM} |
5 | 5 | ||
6 | # If SIGNAL is received, switch to next display | 6 | # If SIGNAL is received, switch to next display |
7 | trap 'next_display' $SIGNAL | 7 | trap 'next_display' "$SIGNAL" |
8 | #trap 'toggle_timer' SIGTSTP | 8 | trap 'toggle_timer' SIGTSTP |
9 | # Do not print "^C" when SIGINT caught | 9 | # Do not print "^C" when SIGINT caught |
10 | stty -ctlecho | 10 | stty -ctlecho |
11 | 11 | ||
@@ -16,16 +16,16 @@ display_list=(STOPWATCH COUNTDOWN PERIOD) | |||
16 | export DISPLAY=0 | 16 | export DISPLAY=0 |
17 | 17 | ||
18 | next_display() { | 18 | next_display() { |
19 | export DISPLAY=$(( ($DISPLAY + 1) %${#display_list[@]} )) | 19 | export DISPLAY=$(( ("$DISPLAY" + 1) %${#display_list[@]} )) |
20 | } | 20 | } |
21 | 21 | ||
22 | export stop= | 22 | export stop=0 |
23 | toggle_timer() { | 23 | toggle_timer() { |
24 | [ "$stop" = true ] && export stop= || export stop=true | 24 | export stop=$(( ("$stop" + 1) %2 )) |
25 | } | 25 | } |
26 | 26 | ||
27 | # Wait user input | 27 | # Wait user input |
28 | read -p '? ' input | 28 | read -p '? ' -r input |
29 | 29 | ||
30 | # Modify input to fit the format that `date` can read | 30 | # Modify input to fit the format that `date` can read |
31 | # s -> sec | 31 | # s -> sec |
@@ -35,28 +35,41 @@ read -p '? ' input | |||
35 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" | 35 | [[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" |
36 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" | 36 | [[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" |
37 | 37 | ||
38 | start=$(date +%s) # unix epoch of start | 38 | # seconds user set |
39 | given=$(date +%s -d "$input") # unix epoch of end | 39 | SET=$(( $(date +%s -d "$input") - $(date +%s) )) |
40 | period=$(( $given - $start )) # seconds for timer | 40 | # seconds pass |
41 | count=0 | ||
41 | 42 | ||
42 | time=0 | 43 | timer() { |
43 | while [ $time -ne $period ]; do | 44 | start=$(date +%s) |
44 | [ "$stop" = true ] && sleep 0.3 && continue | 45 | count_from=$count |
45 | 46 | ||
46 | time="$(( $(date +%s) - $start ))" | 47 | while [ $count -lt $SET ]; do |
48 | |||
49 | # If signal for stop is receive, stop counting | ||
50 | [ $stop = 1 ] && sleep 0.3 && break | ||
51 | |||
52 | count=$(( count_from + $(date +%s) - start )) | ||
47 | case ${display_list[$DISPLAY]} in | 53 | case ${display_list[$DISPLAY]} in |
48 | STOPWATCH) | 54 | STOPWATCH) |
49 | printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" | 55 | printf '%s\r' "$(date -u -d @$count +%H:%M:%S)" |
50 | ;; | 56 | ;; |
51 | COUNTDOWN) | 57 | COUNTDOWN) |
52 | printf '%s\r' "$(date -u -d @$((period - $time)) +%H:%M:%S)" | 58 | printf '%s\r' "$(date -u -d @$((SET - count)) +%H:%M:%S)" |
53 | ;; | 59 | ;; |
54 | PERIOD) | 60 | PERIOD) |
55 | printf '%s\r' "$(date -u -d @$period +%H:%M:%S)" | 61 | printf '%s\r' "$(date -u -d @$SET +%H:%M:%S)" |
56 | ;; | 62 | ;; |
57 | esac | 63 | esac |
58 | 64 | ||
59 | sleep 0.3 | 65 | sleep 0.3 |
66 | done | ||
67 | } | ||
68 | |||
69 | while [ $count -lt $SET ]; do | ||
70 | [ $stop = true ] && sleep 0.3 && continue | ||
71 | timer | ||
60 | done | 72 | done |
61 | 73 | ||
62 | printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" | 74 | # Print bold red text of time that user set |
75 | printf "\e[1;31m%s" "$(date -u -d "@$SET" +%H:%M:%S)" | ||