-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressBar.py
125 lines (105 loc) · 4.4 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
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
import time, sys, os, math
# Class: ProgressBar
# Role: print a progress bar with next to no
# line of code required by user
class ProgressBar:
# Role : assign arguments to internal values
def __init__(self,
pretext=r"", # Text to print before the bar
progresschar=r"█", # Character of done part of the bar
remainingbarchar=r" ", # Character to fill the remaining bar with
loadingchars=r"█▓▒░▒▓", # Last character of bar moving as bar loads (moves even if no progress)
startendchar=r"||", # The two characters going around the bar
displaypercentage=True, # Show percentage as well or not
displaycount=False, # Show count as well or not
rightjustified=True, # Print the bar on the right hand side of the console
consolewidthrate=3 # Print the bar on 1/3 of the width of console
):
self.pretext = str(pretext)
self.progresschar = str(progresschar)
self.remainingbarchar = str(remainingbarchar)
self.loadingchars = loadingchars
self.startendchar = str(startendchar)
self.displaypercentage = displaypercentage
self.displaycount = displaycount
self.rightjustified = rightjustified
self.consolewidthrate = consolewidthrate
# Private
self.loadingcharsindex = 0
self.firstprint = True
# Role : prints the progress bar as an independent thread
# Arguments:
# - number: progress value (type multiprocessing.Value of int)
# - max: value to reach (int)
# - updateperiod: refresh period of progress bar in seconds
def inThread(self, number, max, updateperiod=0.1):
while(number.value < max):
self.print(number.value,max)
time.sleep(float(updateperiod))
self.print(max,max)
# Role : prints the progress bar inline
# Arguments:
# - number: progress value (int)
# - max: maximum value (int)
def print(self,number,max):
actuallyjustifyright = True
consolewidth = os.get_terminal_size().columns
prebarstring = barstring = ""
# No carriage return on first print
if not self.firstprint:
prebarstring += "\r"
self.firstprint = False
### Pre progress bar
if self.pretext:
compatiblepretext = self.pretext
limitsizepretext = math.floor( (1-1/self.consolewidthrate)*consolewidth )
if self.displaycount: limitsizepretext = limitsizepretext - 4 - len(str(max))*2
if self.displaypercentage: limitsizepretext = limitsizepretext - 5
limitsizepretext = limitsizepretext - len(self.startendchar) - 2
limitsizepretext = limitsizepretext - 1
if len(compatiblepretext) >= limitsizepretext:
# If pretext is too long
compatiblepretext = compatiblepretext[:limitsizepretext]+"..."
actuallyjustifyright = False
prebarstring += compatiblepretext + " "
### Progress bar
# Start char
if self.startendchar:
barstring += self.startendchar[0]
# Current state of affairs
barwidth=int(os.get_terminal_size().columns/self.consolewidthrate) # Calculated from terminal size
sofarbar = int( (number/max)*barwidth )
remainingbar = barwidth - sofarbar
# Add progress chars
barstring += sofarbar*self.progresschar
# If loading chars, print loading chars and go to next one (unless 100%)
if self.loadingchars != "" and number != max:
barstring += self.loadingchars[self.loadingcharsindex]
self.loadingcharsindex = (self.loadingcharsindex+1) % len(self.loadingchars)
remainingbar -= 1
# Add remaining gap
barstring += remainingbar*self.remainingbarchar
# End char
if self.startendchar:
if len(self.startendchar) >= 2:
barstring += self.startendchar[1]
else:
barstring += self.startendchar[0]
### Post progress bar
if self.displaypercentage:
per = " %d%%" % int(number*100/max)
barstring += " "*(5 - len(per)) + per
if self.displaycount:
count = " (%d/%d)" % (number,max)
barstring += " "*(len(str(max))-len(str(number))) + count
# Print the bar out
if self.rightjustified and actuallyjustifyright:
fillthevoid = " "*(consolewidth-len(prebarstring)-len(barstring)-1)
sys.stdout.write(prebarstring + fillthevoid + barstring + " ")
sys.stdout.flush()
else:
sys.stdout.write(prebarstring + barstring)
sys.stdout.flush()
# Add new line if bar is finished
if number == max:
print()