-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonowatch.py
47 lines (40 loc) · 1.64 KB
/
monowatch.py
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
from __future__ import print_function
import time
import sys
import datetime
try:
input = raw_input # py2
except NameError:
pass # py3
def monowatch(showdocs=True):
if showdocs:
pausetext = "\nStopwatch paused. KeyboardInterrupt (^C) again to stop. Input numbers to offset the stopwatch ('-10' for 10 seconds subtracted, '10' for 10 seconds added). Input 'u' to resume as if never paused. Press Return to resume stopwatch: "
else:
pausetext = "\nStopwatch paused. Input offset, 'u', or ^C: "
timestart = time.time()
while True:
try:
i = str(datetime.timedelta(seconds=time.time() - timestart))
sys.stdout.write("\r%s" % i)
time.sleep(0.05)
sys.stdout.flush()
except KeyboardInterrupt:
try:
pausedtime = time.time() - timestart
while True:
try:
offset = input(pausetext)
timestart = time.time() - (pausedtime + float(offset))
break
except ValueError:
if offset == "":
timestart = time.time() - pausedtime
break
elif offset.lower() == "u":
break
print("Error, you must input a number to offset and resume, or nothing to just resume, or 'u' to resume as if never paused, or ^C to stop.")
except KeyboardInterrupt:
print("\n")
return datetime.timedelta(seconds=pausedtime)
if __name__ == "__main__":
monowatch()