aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/unix/timer.sh
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-03-20 21:46:49 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-03-20 21:47:08 +0800
commita5e1492da2997072eda4aa53e4acb5d2c287426d (patch)
tree5a0687412ab94fa86cc3f02db555fb449c1868d6 /bin/unix/timer.sh
parentcccc6479e509d20f082671743fa7baf2b89d029c (diff)
Improve timer.sh
- Change window focus to previous after timer is set - Orignal variable "DISPLAY" was a mistake, it is necessary for xdotool (value is :0 in most of the cased)
Diffstat (limited to 'bin/unix/timer.sh')
-rwxr-xr-xbin/unix/timer.sh12
1 files changed, 9 insertions, 3 deletions
diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh
index ca807e8..3abe701 100755
--- a/bin/unix/timer.sh
+++ b/bin/unix/timer.sh
@@ -2,9 +2,11 @@
2# Ref: https://superuser.com/questions/611538/611582#611582 2# Ref: https://superuser.com/questions/611538/611582#611582
3 3
4SIGNAL=${1:-SIGTERM} 4SIGNAL=${1:-SIGTERM}
5COMMAND="$2"
5 6
6# If SIGNAL is received, switch to next display 7# If SIGNAL is received, switch to next display
7trap 'next_display' "$SIGNAL" 8trap 'next_display' "$SIGNAL"
9# Use SIGTSTP (Ctrl-Z in most of the cases) to stop/restart timer
8trap 'toggle_timer' SIGTSTP 10trap 'toggle_timer' SIGTSTP
9 11
10# Do not print "^C" when SIGINT caught 12# Do not print "^C" when SIGINT caught
@@ -14,10 +16,10 @@ stty -ctlecho
14tput civis 16tput civis
15 17
16display_list=(STOPWATCH COUNTDOWN PERIOD) 18display_list=(STOPWATCH COUNTDOWN PERIOD)
17export DISPLAY=0 19display=0
18 20
19next_display() { 21next_display() {
20 export DISPLAY=$(( ("$DISPLAY" + 1) %${#display_list[@]} )) 22 display=$(( ("$display" + 1) %${#display_list[@]} ))
21} 23}
22 24
23export stop=0 25export stop=0
@@ -29,6 +31,10 @@ toggle_timer() {
29read -p '? ' -r input 31read -p '? ' -r input
30# Disable input on terminal 32# Disable input on terminal
31stty -echo 33stty -echo
34# If COMMAND is given, run it after timer is set
35result="$([ -n "$COMMAND" ] && eval "$COMMAND" 2>&1)"
36notify-send "$result" &>/tmp/openbox
37
32 38
33# Modify input to fit the format that `date` can read 39# Modify input to fit the format that `date` can read
34hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h) 40hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h)
@@ -53,7 +59,7 @@ timer() {
53 [ $stop = 1 ] && sleep 0.3 && break 59 [ $stop = 1 ] && sleep 0.3 && break
54 60
55 count=$(( count_from + $(date +%s) - start )) 61 count=$(( count_from + $(date +%s) - start ))
56 case ${display_list[$DISPLAY]} in 62 case ${display_list[$display]} in
57 STOPWATCH) 63 STOPWATCH)
58 printf '%s\r' "$(date -u -d @$count +%H:%M:%S)" 64 printf '%s\r' "$(date -u -d @$count +%H:%M:%S)"
59 ;; 65 ;;