-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waitbar.py
102 lines (72 loc) · 2.89 KB
/
Waitbar.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
import sys
import time
class Waitbar(object):
def __init__(self, use_eta=False,totalWidth=77):
self.progBar = "[]" # This holds the progress bar string
self.min = 0.0
self.max = 1.0
self.span = self.max-self.min
self.width = totalWidth
self.display_eta=True
self.amount = 0 # When amount == max, we are 100% done
self.update(0) # Build progress bar string
self.initial_time=time.time()
def update(self, newAmount = 0):
if newAmount < self.min: newAmount = self.min
if newAmount > self.max: newAmount = self.max
self.amount = newAmount
# Figure out the new percent done, round to an integer
diffFromMin = float(self.amount - self.min)
percentDone = (diffFromMin / float(self.span)) * 100.0
if self.display_eta:
if percentDone==0.0:
eta_str=' (ETA: ??)'
else:
dt=time.time()-self.initial_time
eta=dt/percentDone*(100-percentDone)
seconds=eta%60
eta=int(eta/60)
minutes=eta%60
eta=int(eta/60)
hours=eta%24
eta=int(eta/24)
days=eta
eta_str=' (ETA: '
if days:
eta_str+=str(days)+" d "
if hours:
eta_str+=str(hours)+" h "
if minutes:
eta_str+=str(minutes)+" m "
eta_str+='%.1f s)' % seconds
else:
eta_str=''
percentDone = round(percentDone)
percentDone = int(percentDone)
done_str=str(percentDone)+"%" +eta_str
# Figure out how many hash bars the percentage should be
allFull = self.width - 2
numHashes = (percentDone / 100.0) * allFull
numHashes = int(round(numHashes))
# build a progress bar with hashes and spaces
self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
# figure out where to put the percentage, roughly centered
percentPlace = (len(self.progBar) / 2) - len(done_str)/2
percentString = done_str
# slice the percentage into the bar
self.progBar = self.progBar[0:percentPlace] + percentString + self.progBar[percentPlace+len(percentString):]
def display(self):
print self,
sys.stdout.flush()
def updated(self,newAmount=0):
self.update(newAmount)
self.display()
def __str__(self):
return str(self.progBar)+"\r"
if __name__=="__main__":
import time
w = Waitbar(False)
for i in xrange(1000):
w.update(i/1000.0)
print w,
time.sleep(.01)