diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-27 21:40:46 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-27 21:40:46 +0800 |
commit | 73826942dc1b368bb76f8471d50dffd0e1a4b42f (patch) | |
tree | 256ed9b599c19ed307ae57951481d5e5f0e3adb8 /bin/unix | |
parent | 61281d2f806fa118a1c91f40208e3e144b353078 (diff) |
Improve timer
Diffstat (limited to 'bin/unix')
-rwxr-xr-x | bin/unix/timer.sh | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh index ea15985..ca807e8 100755 --- a/bin/unix/timer.sh +++ b/bin/unix/timer.sh | |||
@@ -6,6 +6,7 @@ SIGNAL=${1:-SIGTERM} | |||
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 | |||
9 | # Do not print "^C" when SIGINT caught | 10 | # Do not print "^C" when SIGINT caught |
10 | stty -ctlecho | 11 | stty -ctlecho |
11 | 12 | ||
@@ -26,17 +27,19 @@ toggle_timer() { | |||
26 | 27 | ||
27 | # Wait user input | 28 | # Wait user input |
28 | read -p '? ' -r input | 29 | read -p '? ' -r input |
30 | # Disable input on terminal | ||
31 | stty -echo | ||
29 | 32 | ||
30 | # Modify input to fit the format that `date` can read | 33 | # Modify input to fit the format that `date` can read |
31 | # s -> sec | 34 | hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h) |
32 | # m -> min | 35 | min=$(grep -o '[0-9.]\+m' <<<"$input" | tr -d m) |
33 | # h -> hour | 36 | sec=$(grep -o '[0-9.]\+s' <<<"$input" | tr -d s) |
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 | 37 | ||
38 | # seconds user set | 38 | # seconds user set |
39 | SET=$(( $(date +%s -d "$input") - $(date +%s) )) | 39 | SET=$( echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc ) |
40 | # Make sure the value is integer, prevent fail in shell comparison | ||
41 | SET=${SET%%.*} | ||
42 | |||
40 | # seconds pass | 43 | # seconds pass |
41 | count=0 | 44 | count=0 |
42 | 45 | ||