From 73826942dc1b368bb76f8471d50dffd0e1a4b42f Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 27 Feb 2023 21:40:46 +0800 Subject: Improve timer --- X11/openbox/rc.xml | 25 +++++++++++++++++-------- bin/unix/timer.sh | 17 ++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/X11/openbox/rc.xml b/X11/openbox/rc.xml index aa58b85..96e271e 100644 --- a/X11/openbox/rc.xml +++ b/X11/openbox/rc.xml @@ -499,14 +499,23 @@ - alacritty --title TIMER --hold - -o "window.dimensions.columns=8" - -o "window.dimensions.lines=1" - -o "window.position.x=-0" - -o "window.position.y=0" - -o "window.opacity=0.6" - -o "font.size=40" - -e ~/helper/bin/unix/timer.sh SIGINT + sh -c ' + xdotool search --name "TIMER" windowactivate || \ + alacritty --title TIMER --hold \ + -o "window.dimensions.columns=8" \ + -o "window.dimensions.lines=1" \ + -o "window.position.x=-0" \ + -o "window.position.y=0" \ + -o "window.opacity=0.6" \ + -o "font.size=40" \ + -e ~/helper/bin/unix/timer.sh SIGINT + ' + + + + + + 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} # If SIGNAL is received, switch to next display trap 'next_display' "$SIGNAL" trap 'toggle_timer' SIGTSTP + # Do not print "^C" when SIGINT caught stty -ctlecho @@ -26,17 +27,19 @@ toggle_timer() { # Wait user input read -p '? ' -r input +# Disable input on terminal +stty -echo # Modify input to fit the format that `date` can read -# s -> sec -# m -> min -# h -> hour -[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")" -[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")" -[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")" +hour=$(grep -o '[0-9.]\+h' <<<"$input" | tr -d h) +min=$(grep -o '[0-9.]\+m' <<<"$input" | tr -d m) +sec=$(grep -o '[0-9.]\+s' <<<"$input" | tr -d s) # seconds user set -SET=$(( $(date +%s -d "$input") - $(date +%s) )) +SET=$( echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc ) +# Make sure the value is integer, prevent fail in shell comparison +SET=${SET%%.*} + # seconds pass count=0 -- cgit v1.2.3-70-g09d2