-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation-timer
executable file
·67 lines (51 loc) · 1.64 KB
/
presentation-timer
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
#!/usr/bin/env python
"""
Print a non-distracting terminal clock useful for public speaking.
This program outputs elapsed minutes and seconds so you can tell how long
you've been talking. It's especially good at large terminal font sizes.
"""
VERSION = '0.2.0'
import optparse
import os
import sys
import time
from datetime import datetime
class Timer(object):
def __init__(self, stream=sys.stdout):
self.stream = stream
def run(self, interval=1):
os.system('clear')
self.stream.write('\n')
os.system('tput civis')
try:
self._loop(interval=interval)
finally:
os.system('tput cnorm')
def _loop(self, interval=1):
start = datetime.now()
while True:
self.stream.write('\r ')
self.stream.write(self.format_delta(datetime.now() - start))
self.stream.flush()
time.sleep(interval)
def format_delta(self, delta):
s = ''
if delta.days:
s += str(delta.days) + ' '
s += 'days ' if delta.days > 1 else 'day '
s += "%d:%02d" % ((delta.seconds // 60) % 60,
delta.seconds % 60)
return s
def main():
p = optparse.OptionParser(usage = '%prog [options]\n' + __doc__.rstrip(),
version = '%prog ' + VERSION)
p.add_option('-i', '--interval', dest='interval', default=5,
help='interval between updates')
opts, args = p.parse_args()
t = Timer()
try:
t.run(interval=int(opts.interval))
except KeyboardInterrupt:
print ''
if __name__ == '__main__':
main()