-
Notifications
You must be signed in to change notification settings - Fork 13
/
countdown
executable file
·73 lines (64 loc) · 2.21 KB
/
countdown
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# countdown -- like 'sleep', but display a running countdown
import argparse
import enum
import re
import sys
import time
sys.path.append(sys.path[0] + "/../lib/python")
from nullroute.string import fmt_duration, parse_duration
# Modes for OSC 9;4
class Progress(enum.IntEnum):
REMOVE = 0
NORMAL = 1
ERROR = 2
THROB = 3
PAUSED = 4
def out(msg):
print("\r\033[K%s" % msg, end="", flush=True)
def outprog(mode, value=0):
print("\033]9;4;%d;%d\033\\" % (mode, value), end="", flush=True)
def _fmt_duration(t):
return fmt_duration(round(t))
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--taskbar", action="store_true",
help="show progress in taskbar")
parser.add_argument("duration", nargs="*",
help="countdown duration")
args, rest = parser.parse_known_args()
secs = sum(map(parse_duration, args.duration + rest))
if secs > 0:
# Positive argument = count down from the specified time to zero
start = time.time()
finish = start + secs
try:
while time.time() < finish:
if args.taskbar:
outprog(Progress.NORMAL, 100 - (secs / (finish - start) * 100))
out("waiting (%s left) " % _fmt_duration(finish - time.time()))
next = min(secs, 1)
secs -= next
time.sleep(next)
if args.taskbar:
outprog(Progress.REMOVE)
out("done after %s\n" % _fmt_duration(time.time() - start))
except KeyboardInterrupt:
if args.taskbar:
outprog(Progress.REMOVE)
out("break after %s (%s left)\n" % (_fmt_duration(time.time() - start),
_fmt_duration(finish - time.time())))
exit(1)
else:
# Zero or negative argument = count up to infinity
start = time.time() - abs(secs)
try:
while True:
if args.taskbar:
outprog(Progress.THROB)
out("waiting (%s elapsed) " % _fmt_duration(time.time() - start))
time.sleep(1)
except KeyboardInterrupt:
if args.taskbar:
outprog(Progress.REMOVE)
out("break after %s\n" % _fmt_duration(time.time() - start))
exit(1)