aboutsummaryrefslogtreecommitdiffhomepage
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
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)
-rw-r--r--X11/openbox/rc.xml18
-rwxr-xr-xbin/unix/timer.sh12
2 files changed, 19 insertions, 11 deletions
diff --git a/X11/openbox/rc.xml b/X11/openbox/rc.xml
index e7736f8..d7c0345 100644
--- a/X11/openbox/rc.xml
+++ b/X11/openbox/rc.xml
@@ -499,15 +499,17 @@
499 <action name="Execute"> 499 <action name="Execute">
500 <command> 500 <command>
501 sh -c ' 501 sh -c '
502 origin=`xdotool getactivewindow`; \
502 xdotool search --name "TIMER" windowactivate || \ 503 xdotool search --name "TIMER" windowactivate || \
503 alacritty --title TIMER --hold \ 504 alacritty --title TIMER --hold \
504 -o "window.dimensions.columns=8" \ 505 -o "window.dimensions.columns=8" \
505 -o "window.dimensions.lines=1" \ 506 -o "window.dimensions.lines=1" \
506 -o "window.position.x=-0" \ 507 -o "window.position.x=-0" \
507 -o "window.position.y=0" \ 508 -o "window.position.y=0" \
508 -o "window.opacity=0.6" \ 509 -o "window.opacity=0.6" \
509 -o "font.size=40" \ 510 -o "font.size=40" \
510 -e ~/helper/bin/unix/timer.sh SIGINT 511 -e ~/helper/bin/unix/timer.sh SIGINT \
512 "xdotool windowactivate $origin"
511 ' 513 '
512 </command> 514 </command>
513 </action> 515 </action>
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 ;;