From e8354fe44d01714d0a1b8a40d1f3d352dd2cb876 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 22 Feb 2023 22:17:04 +0800 Subject: Update --- bin/unix/countdown.sh | 19 +++++++++++++++++-- bin/unix/stopwatch.sh | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'bin/unix') diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh index 683949e..ca6b5d6 100755 --- a/bin/unix/countdown.sh +++ b/bin/unix/countdown.sh @@ -1,19 +1,34 @@ #! /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` ))" - printf '%s\r' "$(date -u -d "@$time" +%H:%M:%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 index 70f6b0c..6b76c26 100755 --- a/bin/unix/stopwatch.sh +++ b/bin/unix/stopwatch.sh @@ -1,9 +1,21 @@ #! /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")" @@ -14,7 +26,12 @@ given=$(date +%s -d "$input") stop=$(( $given - $start )) while [ $time -ne $stop ]; do time="$(( $(date +%s) - $start ))" - printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" + 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)" -- cgit v1.2.3-70-g09d2