1- import time , sys , os
1+ import time , sys , os , math
22
33# Class: ProgressBar
44# Role: print a progress bar with next to no
@@ -8,22 +8,24 @@ class ProgressBar:
88 # Role : assign arguments to internal values
99 def __init__ (self ,
1010 pretext = r"" , # Text to print before the bar
11- progresschar = r"█" , # Character to show progress
11+ progresschar = r"█" , # Character of done part of the bar
1212 remainingbarchar = r" " , # Character to fill the remaining bar with
1313 loadingchars = r"█▓▒░▒▓" , # Last character of bar moving as bar loads (moves even if no progress)
1414 startendchar = r"||" , # The two characters going around the bar
15- barwidth = int (os .get_terminal_size ().columns / 2 ), # Length of the bar in characters (does not include what's around the bar)
16- displaypercentage = False , # Show percentage as well or not
17- displaycount = False # Show count as well or not
15+ displaypercentage = True , # Show percentage as well or not
16+ displaycount = False , # Show count as well or not
17+ rightjustified = True , # Print the bar on the right hand side of the console
18+ consolewidthrate = 3 # Print the bar on 1/3 of the width of console
1819 ):
1920 self .pretext = str (pretext )
2021 self .progresschar = str (progresschar )
2122 self .remainingbarchar = str (remainingbarchar )
2223 self .loadingchars = loadingchars
2324 self .startendchar = str (startendchar )
24- self .barwidth = int (barwidth )
2525 self .displaypercentage = displaypercentage
2626 self .displaycount = displaycount
27+ self .rightjustified = rightjustified
28+ self .consolewidthrate = consolewidthrate
2729
2830 # Private
2931 self .loadingcharsindex = 0
@@ -36,7 +38,7 @@ def __init__(self,
3638 # - max: value to reach (int)
3739 # - updateperiod: refresh period of progress bar in seconds
3840 def inThread (self , number , max , updateperiod = 0.1 ):
39- while (number .value != max ):
41+ while (number .value < max ):
4042 self .print (number .value ,max )
4143 time .sleep (float (updateperiod ))
4244 self .print (max ,max )
@@ -47,47 +49,77 @@ def inThread(self, number, max, updateperiod=0.1):
4749 # - number: progress value (int)
4850 # - max: maximum value (int)
4951 def print (self ,number ,max ):
50- barstring = ""
52+ actuallyjustifyright = True
53+ consolewidth = os .get_terminal_size ().columns
54+
55+ prebarstring = barstring = ""
5156
5257 # No carriage return on first print
5358 if not self .firstprint :
54- barstring += "\r "
59+ prebarstring += "\r "
5560 self .firstprint = False
5661
57- # Pre progress bar
62+ ### Pre progress bar
5863 if self .pretext :
59- barstring += self .pretext
64+ compatiblepretext = self .pretext
65+ limitsizepretext = math .floor ( (1 - 1 / self .consolewidthrate )* consolewidth )
66+ if self .displaycount : limitsizepretext = limitsizepretext - 4 - len (str (max ))* 2
67+ if self .displaypercentage : limitsizepretext = limitsizepretext - 5
68+ limitsizepretext = limitsizepretext - len (self .startendchar ) - 2
69+ limitsizepretext = limitsizepretext - 1
70+ if len (compatiblepretext ) >= limitsizepretext :
71+ # If pretext is too long
72+ compatiblepretext = compatiblepretext [:limitsizepretext ]+ "..."
73+ actuallyjustifyright = False
74+ prebarstring += compatiblepretext + " "
75+
76+ ### Progress bar
6077
61- # Progress bar
62- #
6378 # Start char
6479 if self .startendchar :
6580 barstring += self .startendchar [0 ]
81+
6682 # Current state of affairs
67- sofarbar = int ( (number / max )* self .barwidth )
68- remainingbar = self .barwidth - sofarbar
83+ barwidth = int (os .get_terminal_size ().columns / self .consolewidthrate ) # Calculated from terminal size
84+ sofarbar = int ( (number / max )* barwidth )
85+ remainingbar = barwidth - sofarbar
86+
6987 # Add progress chars
7088 barstring += sofarbar * self .progresschar
89+
7190 # If loading chars, print loading chars and go to next one (unless 100%)
7291 if self .loadingchars != "" and number != max :
7392 barstring += self .loadingchars [self .loadingcharsindex ]
7493 self .loadingcharsindex = (self .loadingcharsindex + 1 ) % len (self .loadingchars )
7594 remainingbar -= 1
95+
7696 # Add remaining gap
7797 barstring += remainingbar * self .remainingbarchar
98+
7899 # End char
79100 if self .startendchar :
80101 if len (self .startendchar ) >= 2 :
81102 barstring += self .startendchar [1 ]
82103 else :
83104 barstring += self .startendchar [0 ]
84105
85- # Post progress bar
106+ ### Post progress bar
86107 if self .displaypercentage :
87- barstring += " %d%%" % int (number * 100 / max )
108+ per = " %d%%" % int (number * 100 / max )
109+ barstring += " " * (5 - len (per )) + per
88110 if self .displaycount :
89- barstring += " (%d/%d)" % (number ,max )
111+ count = " (%d/%d)" % (number ,max )
112+ barstring += " " * (len (str (max ))- len (str (number ))) + count
90113
91114 # Print the bar out
92- sys .stdout .write (barstring )
93- sys .stdout .flush ()
115+ if self .rightjustified and actuallyjustifyright :
116+ fillthevoid = " " * (consolewidth - len (prebarstring )- len (barstring )- 1 )
117+ sys .stdout .write (prebarstring + fillthevoid + barstring + " " )
118+ sys .stdout .flush ()
119+ else :
120+ sys .stdout .write (prebarstring + barstring )
121+ sys .stdout .flush ()
122+
123+ # Add new line if bar is finished
124+ if number == max :
125+ print ()
0 commit comments