aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-02-22 15:03:30 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-02-22 15:03:30 +0800
commitd0947320bc1b3a1223097749affe798ba225f979 (patch)
tree7dad38f06475b1757b675f80762d2452e8809cb0 /bin
parentc9fc50031b3f304c58524a3ee524598eed648560 (diff)
Update
Diffstat (limited to 'bin')
-rwxr-xr-xbin/unix/countdown.sh19
-rwxr-xr-xbin/unix/stopwatch.sh20
2 files changed, 39 insertions, 0 deletions
diff --git a/bin/unix/countdown.sh b/bin/unix/countdown.sh
new file mode 100755
index 0000000..683949e
--- /dev/null
+++ b/bin/unix/countdown.sh
@@ -0,0 +1,19 @@
1#! /bin/bash
2# Ref: https://superuser.com/questions/611538/611582#611582
3
4tput civis
5read -p '? ' input
6
7[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")"
8[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")"
9[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")"
10
11
12start=$(date +%s -d "$input")
13while [ "$start" -ge $(date +%s) ]; do
14 ## Is this more than 24h away?
15 time="$(( $start - `date +%s` ))"
16 printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
17 sleep 0.3
18done
19printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)"
diff --git a/bin/unix/stopwatch.sh b/bin/unix/stopwatch.sh
new file mode 100755
index 0000000..70f6b0c
--- /dev/null
+++ b/bin/unix/stopwatch.sh
@@ -0,0 +1,20 @@
1#! /bin/bash
2# Ref: https://superuser.com/questions/611538/611582#611582
3
4tput civis
5read -p '? ' input
6
7[[ "$input" =~ s && ! "$input" =~ sec ]] && input="$(sed s/s/sec/ <<<"$input")"
8[[ "$input" =~ m && ! "$input" =~ min ]] && input="$(sed s/m/min/ <<<"$input")"
9[[ "$input" =~ h && ! "$input" =~ hour ]] && input="$(sed s/h/hour/ <<<"$input")"
10
11time=0
12start=$(date +%s)
13given=$(date +%s -d "$input")
14stop=$(( $given - $start ))
15while [ $time -ne $stop ]; do
16 time="$(( $(date +%s) - $start ))"
17 printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
18 sleep 0.3
19done
20printf "\e[1;31m$(date -u -d "@$time" +%H:%M:%S)"