-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressBar.py
54 lines (43 loc) · 1.72 KB
/
ProgressBar.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
import sys
class ProgressBar:
def __init__(self, _min=0, _max=10, width=12):
# Set base parameters
self.min = _min
self.max = _max
self.span = _max - _min
self.width = width
# When progress == max, we are 100% done
self.progress = 0
# Build the progress bar string and prepare for drawing
self.old_bar = ''
self.progress_bar = "[]"
self.update_progress(0)
def update_progress(self, progress=0):
if progress < self.min:
progress = self.min
if progress > self.max:
progress = self.max
self.progress = progress
# Figure out how many hash bars the percentage should be
done = float(self.progress - self.min) / float(self.span)
hashes = int(round(done * (self.width - 2)))
# build a progress bar with hashes and spaces
self.progress_bar = '[' + \
'#' * hashes + \
' ' * (self.width - hashes - 2) + \
']'
# Slice the percentage into the bar, roughly centered
place = int(self.width / 2 - 1)
percent = str(int(done * 100.0)) + "%"
self.progress_bar = self.progress_bar[0:place] + \
percent + \
self.progress_bar[place + len(percent):]
# Draw as needed
self.draw()
def draw(self):
if self.progress_bar != self.old_bar:
self.old_bar = self.progress_bar
sys.stderr.write(self.progress_bar + '\r')
sys.stderr.flush()
def __str__(self):
return str(self.progress_bar)