aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xbin/unix/timer.sh47
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 @@
4SIGNAL=${1:-SIGTERM} 4SIGNAL=${1:-SIGTERM}
5 5
6# If SIGNAL is received, switch to next display 6# If SIGNAL is received, switch to next display
7trap 'next_display' $SIGNAL 7trap 'next_display' "$SIGNAL"
8#trap 'toggle_timer' SIGTSTP 8trap 'toggle_timer' SIGTSTP
9# Do not print "^C" when SIGINT caught 9# Do not print "^C" when SIGINT caught
10stty -ctlecho 10stty -ctlecho
11 11
@@ -16,16 +16,16 @@ display_list=(STOPWATCH COUNTDOWN PERIOD)
16export DISPLAY=0 16export DISPLAY=0
17 17
18next_display() { 18next_display() {
19 export DISPLAY=$(( ($DISPLAY + 1) %${#display_list[@]} )) 19 export DISPLAY=$(( ("$DISPLAY" + 1) %${#display_list[@]} ))
20} 20}
21 21
22export stop= 22export stop=0
23toggle_timer() { 23toggle_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
28read -p '? ' input 28read -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
38start=$(date +%s) # unix epoch of start 38# seconds user set
39given=$(date +%s -d "$input") # unix epoch of end 39SET=$(( $(date +%s -d "$input") - $(date +%s) ))
40period=$(( $given - $start )) # seconds for timer 40# seconds pass
41count=0
41 42
42time=0 43timer() {
43while [ $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
69while [ $count -lt $SET ]; do
70 [ $stop = true ] && sleep 0.3 && continue
71 timer
60done 72done
61 73
62printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" 74# Print bold red text of time that user set
75printf "\e[1;31m%s" "$(date -u -d "@$SET" +%H:%M:%S)"