diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-20 21:46:49 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-20 21:47:08 +0800 |
commit | a5e1492da2997072eda4aa53e4acb5d2c287426d (patch) | |
tree | 5a0687412ab94fa86cc3f02db555fb449c1868d6 /bin/unix/timer.sh | |
parent | cccc6479e509d20f082671743fa7baf2b89d029c (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-x | bin/unix/timer.sh | 12 |
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 | ||
4 | SIGNAL=${1:-SIGTERM} | 4 | SIGNAL=${1:-SIGTERM} |
5 | COMMAND="$2" | ||
5 | 6 | ||
6 | # If SIGNAL is received, switch to next display | 7 | # If SIGNAL is received, switch to next display |
7 | trap 'next_display' "$SIGNAL" | 8 | trap 'next_display' "$SIGNAL" |
9 | # Use SIGTSTP (Ctrl-Z in most of the cases) to stop/restart timer | ||
8 | trap 'toggle_timer' SIGTSTP | 10 | trap '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 | |||
14 | tput civis | 16 | tput civis |
15 | 17 | ||
16 | display_list=(STOPWATCH COUNTDOWN PERIOD) | 18 | display_list=(STOPWATCH COUNTDOWN PERIOD) |
17 | export DISPLAY=0 | 19 | display=0 |
18 | 20 | ||
19 | next_display() { | 21 | next_display() { |
20 | export DISPLAY=$(( ("$DISPLAY" + 1) %${#display_list[@]} )) | 22 | display=$(( ("$display" + 1) %${#display_list[@]} )) |
21 | } | 23 | } |
22 | 24 | ||
23 | export stop=0 | 25 | export stop=0 |
@@ -29,6 +31,10 @@ toggle_timer() { | |||
29 | read -p '? ' -r input | 31 | read -p '? ' -r input |
30 | # Disable input on terminal | 32 | # Disable input on terminal |
31 | stty -echo | 33 | stty -echo |
34 | # If COMMAND is given, run it after timer is set | ||
35 | result="$([ -n "$COMMAND" ] && eval "$COMMAND" 2>&1)" | ||
36 | notify-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 |
34 | hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h) | 40 | hour=$(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 | ;; |