1
0
mirror of https://github.com/tiyn/dotfiles.git synced 2025-03-18 18:07:45 +01:00

53 lines
1.1 KiB
Plaintext
Raw Normal View History

2023-10-26 04:09:13 +02:00
#!/bin/sh
2023-10-26 04:08:55 +02:00
help="""
usage: timer [option] [args]
options:
-c SECONDS Count down from given amount of seconds
-s Start stopwatch
-h Display this message
"""
2023-10-26 04:29:54 +02:00
max_seconds=86400
2023-10-26 04:08:55 +02:00
countdown() {
start="$(( $(date '+%s') + $1))"
while [ $start -ge $(date +%s) ]; do
time="$(( $start - $(date +%s) ))"
2023-10-26 04:29:54 +02:00
if [ $time -ge $max_seconds ]; then
echo "Maximum number of seconds ($max_seconds) for countdown surpassed."
exit
fi
2023-10-26 04:08:55 +02:00
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
sleep 0.1
done
}
stopwatch() {
start=$(date +%s)
while true; do
time="$(( $(date +%s) - $start))"
printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
2023-10-26 04:29:54 +02:00
if [ $time -ge $max_seconds ]; then
echo "Maximum number of seconds ($max_seconds) reached."
exit
fi
2023-10-26 04:08:55 +02:00
sleep 0.1
done
}
2023-10-26 04:38:53 +02:00
no_args="true"
2023-10-26 04:08:55 +02:00
while getopts "c:sh" arg; do
case $arg in
c) countdown $OPTARG;;
s) stopwatch;;
h) echo "$help";;
2023-10-26 04:38:53 +02:00
*) echo "$help";;
2023-10-26 04:08:55 +02:00
esac
2023-10-26 04:38:53 +02:00
no_args="false"
2023-10-26 04:08:55 +02:00
done
2023-10-26 04:38:53 +02:00
if [ "$no_args" = "true" ]; then
echo "$help"
fi