Enable countdown to take human-readable short times

This commit is contained in:
Daniel Flanagan 2025-01-10 11:28:35 -06:00
parent 604e19a794
commit b7482c6871
2 changed files with 28 additions and 3 deletions

View file

@ -2006,6 +2006,14 @@
"git.hq.bill.com" = {
user = "git";
};
"steam-deck-oled" = {
user = "deck";
hostname = "sdo";
};
"steam-deck" = {
user = "deck";
hostname = "steamdeck";
};
work = {
user = "daniel.flanagan";
};

View file

@ -3,15 +3,32 @@
function usage {
echo "countdown - exit after a certain amount of time has passed"
echo " Usage:"
echo " countdown <SECONDS> && command..."
echo " countdown <TIME> && command..."
echo
echo " Examples:"
echo ' countdown 120 && echo "Two minutes has elapsed!"'
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!"'
}
[[ $# -lt 1 ]] && { printf "error: no SECONDS argument provided\n" >&2; usage; exit 1; }
d=$(($(date +%s) + $1));
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));
printf 'Started at %s\n' "$(date)"
while [[ "$d" -ge "$(date +%s)" ]]; do