nix/modules/home-manager/scripts/common/bin/countdown

42 lines
1 KiB
Text
Raw Normal View History

2023-09-05 13:52:52 -05:00
#!/usr/bin/env bash
2024-04-26 11:35:28 -05:00
function usage {
echo "countdown - exit after a certain amount of time has passed"
echo " Usage:"
echo " countdown <TIME> && command..."
2024-04-26 11:35:28 -05:00
echo
echo " Examples:"
echo ' countdown 120 && echo "Two minutes have elapsed!"'
echo ' countdown 5m && echo "Five minutes have elapsed!"'
echo ' countdown 10h && echo "Ten hours have elapsed!"'
echo ' countdown 9d && echo "Nine days have elapsed!"'
2024-04-26 11:35:28 -05:00
}
[[ $# -lt 1 ]] && { printf "error: no SECONDS argument provided\n" >&2; usage; exit 1; }
2023-09-05 13:52:52 -05:00
t="$1"
seconds="$(echo "$t" | tr -d -c 0-9)"
if [[ $t =~ ^.*m$ ]]; then
seconds=$((seconds * 60))
fi
if [[ $t =~ ^.*h$ ]]; then
seconds=$((seconds * 60 * 60))
fi
if [[ $t =~ ^.*d$ ]]; then
seconds=$((seconds * 60 * 60 * 24))
fi
d=$(($(date +%s) + seconds));
2024-04-26 11:35:28 -05:00
printf 'Started at %s\n' "$(date)"
2023-09-05 13:52:52 -05:00
while [[ "$d" -ge "$(date +%s)" ]]; do
_dt=$((d - $(date +%s)))
days=$((_dt / 86400))
2024-04-26 11:35:28 -05:00
printf "\r%sd %s " "$days" "$(date -u --date @$((_dt)) +%H:%M:%S)";
2023-09-05 13:52:52 -05:00
sleep 0.1
done
2024-04-26 11:35:28 -05:00
printf "\rCountdown finished %s\n" "$(date)"