From c4377f99fbbb8c95be46b4c6399d1db376688940 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 27 Feb 2023 16:04:51 +0800 Subject: Update timer.sh --- bin/unix/countdown.sh | 34 ---------------------------- bin/unix/stopwatch.sh | 37 ------------------------------ bin/unix/timer.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 71 deletions(-) delete mode 100755 bin/unix/countdown.sh delete mode 100755 bin/unix/stopwatch.sh create mode 100755 bin/unix/timer.sh (limited to 'bin/unix') diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh deleted file mode 100755 index ca6b5d6..0000000 --- a/bin/unix/countdown.sh +++ /dev/null @@ -1,34 +0,0 @@ -#! /bin/bash -# Ref: https://superuser.com/questions/611538/611582#611582 - -# If user type '^C', then use SIGINT to toggle the display -trap '[ "$SHOWGIVEN" = yes ] && SHOWGIVEN= || SHOWGIVEN=yes' SIGINT -# Do not print "^C" when SIGINT caught -stty -ctlecho - -# Hide the cursor -tput civis - -# Wait user input -read -p '? ' input - -# 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")" - -start=$(date +%s -d "$input") -while [ "$start" -ge $(date +%s) ]; do - ## Is this more than 24h away? - time="$(( $start - `date +%s` ))" - if [ -z "$SHOWGIVEN" ]; then - printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" - else - printf '%s\r' "$(date -d "00 + $input" +%H:%M:%S)" - fi - sleep 0.3 -done -printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh deleted file mode 100755 index 6b76c26..0000000 --- a/bin/unix/stopwatch.sh +++ /dev/null @@ -1,37 +0,0 @@ -#! /bin/bash -# Ref: https://superuser.com/questions/611538/611582#611582 - -# If user type '^C', then use SIGINT to toggle the display -trap '[ "$SHOWGIVEN" = yes ] && SHOWGIVEN= || SHOWGIVEN=yes' SIGINT -# Do not print "^C" when SIGINT caught -stty -ctlecho - -# Hide the cursor -tput civis - -# Wait user input -read -p '? ' input - -# 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")" - -time=0 -start=$(date +%s) -given=$(date +%s -d "$input") -stop=$(( $given - $start )) -while [ $time -ne $stop ]; do - time="$(( $(date +%s) - $start ))" - if [ -z "$SHOWGIVEN" ]; then - printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" - else - printf '%s\r' "$(date -u -d @$stop +%H:%M:%S)" - fi - sleep 0.3 -done - -printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh new file mode 100755 index 0000000..ac9c858 --- /dev/null +++ b/bin/unix/timer.sh @@ -0,0 +1,62 @@ +#! /bin/bash +# Ref: https://superuser.com/questions/611538/611582#611582 + +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 + +# Hide the cursor +tput civis + +display_list=(STOPWATCH COUNTDOWN PERIOD) +export DISPLAY=0 + +next_display() { + export DISPLAY=$(( ($DISPLAY + 1) %${#display_list[@]} )) +} + +export stop= +toggle_timer() { + [ "$stop" = true ] && export stop= || export stop=true +} + +# Wait user input +read -p '? ' input + +# 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")" + +start=$(date +%s) # unix epoch of start +given=$(date +%s -d "$input") # unix epoch of end +period=$(( $given - $start )) # seconds for timer + +time=0 +while [ $time -ne $period ]; do + [ "$stop" = true ] && sleep 0.3 && continue + + time="$(( $(date +%s) - $start ))" + case ${display_list[$DISPLAY]} in + STOPWATCH) + printf '%s\r' "$(date -u -d @$time +%H:%M:%S)" + ;; + COUNTDOWN) + printf '%s\r' "$(date -u -d @$((period - $time)) +%H:%M:%S)" + ;; + PERIOD) + printf '%s\r' "$(date -u -d @$period +%H:%M:%S)" + ;; + esac + + sleep 0.3 +done + +printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)" -- cgit v1.2.3-70-g09d2