aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-02-27 21:40:46 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-02-27 21:40:46 +0800
commit73826942dc1b368bb76f8471d50dffd0e1a4b42f (patch)
tree256ed9b599c19ed307ae57951481d5e5f0e3adb8 /bin
parent61281d2f806fa118a1c91f40208e3e144b353078 (diff)
Improve timer
Diffstat (limited to 'bin')
-rwxr-xr-xbin/unix/timer.sh17
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
7trap 'next_display' "$SIGNAL" 7trap 'next_display' "$SIGNAL"
8trap 'toggle_timer' SIGTSTP 8trap 'toggle_timer' SIGTSTP
9
9# Do not print "^C" when SIGINT caught 10# Do not print "^C" when SIGINT caught
10stty -ctlecho 11stty -ctlecho
11 12
@@ -26,17 +27,19 @@ toggle_timer() {
26 27
27# Wait user input 28# Wait user input
28read -p '? ' -r input 29read -p '? ' -r input
30# Disable input on terminal
31stty -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 34hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h)
32# m -> min 35min=$(grep -o '[0-9.]\+m' <<<"$input" | tr -d m)
33# h -> hour 36sec=$(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
39SET=$(( $(date +%s -d "$input") - $(date +%s) )) 39SET=$( echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc )
40# Make sure the value is integer, prevent fail in shell comparison
41SET=${SET%%.*}
42
40# seconds pass 43# seconds pass
41count=0 44count=0
42 45