This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
progress.py
55 lines (46 loc) · 1.51 KB
/
progress.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
class ProgressCounter(object):
def __init__(self, start, target, terminal):
self.terminal = terminal
self.start = start
self.target = target
self.current = self.start
def tick(self, erase_line=True):
self.current += 1
n = self.current - self.start
d = self.target - self.start
pct = round(float(n) * 100 / float(d), 2)
if erase_line:
self.terminal.write("\x1B[2K")
self.terminal.write("\x1B[1G")
self.terminal.write("%d / %d (%s%%) " % (n, d, pct))
def done(self):
pass
def percentage(self):
return (self.current - self.start) / (self.target - self.start)
def isdone(self):
return self.current >= self.target
class ProgressSpinner(object):
def __init__(self, terminal):
self.progress_chars = list(['|', '/', '-', '\\'])
self.count = 0
self.terminal = terminal
def tick(self):
self.terminal.write("\x1B[1D")
self.terminal.write(self.progress_chars[self.count % len(self.progress_chars)])
self.terminal.flush()
self.count += 1
def done(self):
self.terminal.write("\x1B[1D")
self.terminal.write('Done\n')
self.terminal.flush()
if __name__ == "__main__":
import sys
import time
spinner = ProgressSpinner(sys.stdout)
try:
while spinner.count < 100:
spinner.tick()
time.sleep(0.1)
spinner.done()
except KeyboardInterrupt:
spinner.done()