-
Notifications
You must be signed in to change notification settings - Fork 0
/
termtimer.py
executable file
·145 lines (129 loc) · 4.46 KB
/
termtimer.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Timer in Terminal
# Desenvolvido por: Marcelo Fonseca Tambalo
# https://github.com/zokis/TermTimer
import time as timer
import os
import sys
import optparse
import platform
from pygments import console
from utils.fonte import FonteZokis
EXIT = 0
ERROR = 2
class TermTimer(object):
def __init__(self, time=None, notnow=True, in_sec=False, times=None):
console.codes['bg_gray'] = '\x1b[01;40m'
self.font = FonteZokis()
self.timing = 5
if times and times <= 1:
print ('Usage: termtimer.py [options]\n' +
'\n' +
'termtimer.py: error: -x option require a value greater than 1')
sys.exit(ERROR)
self.times_fix = times and times - 1 or None
self.times = self.times_fix
self.cls = self.font.cls
if time:
self.timing = time
if not in_sec:
self.timing = int(self.timing * 60)
if notnow:
raw_input("Press Enter")
else:
self.in_set_time()
def in_set_time(self):
try:
print (console.colorize('green', 'Enter the time for timing'))
print (console.colorize('blue', '0') +
console.colorize('green', ' to') +
console.colorize('blue', ' exit'))
print (console.colorize('green', 'nothing to ') +
console.colorize('blue', '5 ') +
console.colorize('green', 'minutes'))
timing = raw_input(console.colorize('red', '>> '))
except:
sys.exit(ERROR)
if timing == '0':
sys.exit(EXIT)
try:
if timing:
timing = float(timing)
else:
timing = 5
except:
sys.exit(ERROR)
if timing < 0:
timing *= -1
self.timing = int(timing * 60)
def end(self):
str_fim = ' Time Out! ' * 5
self.cls()
if platform.system() == 'Linux':
os.system("notify-send '%s'" % str_fim)
else:
print "%s" % str_fim
timer.sleep(0.5)
self.cls()
if self.times <= 0:
self.restart()
else:
self.times -= 1
self.clocking()
def restart(self):
self.cls()
print "%s" % console.reset_color()
self.cls()
op = raw_input(console.colorize('green', 'Timing again? [the options remained the same] ') +
console.colorize('turquoise', '(y/n)') +
console.colorize('green', ': '))
if op in ("y", "Y"):
if self.times_fix:
self.times = self.times_fix
self.clocking()
elif op in ("n", "N"):
print console.reset_color()
self.cls()
sys.exit(EXIT)
else:
self.restart()
def get_progress_bar(self, time):
size = 37
try:
rest = int(time * size / self.timing)
except ZeroDivisionError:
rest = 0
progress = size - rest
return "[%s>%s]" % ("=" * progress, "-" * rest)
def clocking(self):
print console.codes['bg_gray']
for i in range(self.timing, -1, -1):
self.cls()
print self.font.text_renderer("%0.2d" % (i / 60) +
":" + "%0.2d" % (i % 60))
print self.get_progress_bar(i)
timer.sleep(1.0)
self.end()
def main(argv):
opar = optparse.OptionParser()
opar.add_option("-s", "--sec", dest="sec",
help="time in seconds", type="int", default=None)
opar.add_option("-m", "--min", dest="min",
help="time in minutes", type="float", default=None)
opar.add_option("-n", "--notnow", dest="notnow",
help="the timer does not start now", default=False,
action="store_true")
opar.add_option("-x", "--times", dest="times",
help="number(greater than 1) of times that the timer will run", type="int", default=None)
options, args = opar.parse_args(argv)
if options.sec:
tc = TermTimer(options.sec, options.notnow, True, options.times)
elif options.min:
tc = TermTimer(options.min, options.notnow, False, options.times)
else:
tc = TermTimer(times=options.times)
tc.clocking()
sys.exit(EXIT)
if __name__ == '__main__':
main(sys.argv[1:])