summaryrefslogtreecommitdiff
path: root/etc/functions/countdown
blob: 28639f4283145b2816f58a4e9388caa9978d3821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## vim:ft=zsh
## Countdown to a specific date/time
## Usage: countdown <string understood by 'date -d'>
##    or: countdown +<seconds>
## Copyright (C) 2008-2009 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>

autoload check_com
typeset -i beep=0
typeset -i seconds
typeset date

if check_com beep; then
	beep=1
fi

if [[ $1 == +* ]] {
	date=@$(($(date +%s) + ${1#+}))
} else {
	date="$*"
}

while true; do
	seconds=$[$(date -d $date +%s)-$(date +%s)]
	printf '%02d:%02d:%02d' $((seconds/3600)) $(((seconds/60)%60)) $((seconds%60))
	if (( seconds <= 0 )) {
		(( beep )) && beep || echo -ne "\a"
	}
	sleep 1
	echo -ne "\r\e[2K"
done