Skip to content

Commit

Permalink
Now justify right, choose console width rate
Browse files Browse the repository at this point in the history
  • Loading branch information
oldabl authored Apr 10, 2024
1 parent 4948b1b commit c0dd11c
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions ProgressBar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time, sys, os
import time, sys, os, math

# Class: ProgressBar
# Role: print a progress bar with next to no
Expand All @@ -8,22 +8,24 @@ class ProgressBar:
# Role : assign arguments to internal values
def __init__(self,
pretext=r"", # Text to print before the bar
progresschar=r"█", # Character to show progress
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
barwidth=int(os.get_terminal_size().columns/2), # Length of the bar in characters (does not include what's around the bar)
displaypercentage=False, # Show percentage as well or not
displaycount=False # Show count as well or not
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.barwidth = int(barwidth)
self.displaypercentage = displaypercentage
self.displaycount = displaycount
self.rightjustified = rightjustified
self.consolewidthrate = consolewidthrate

# Private
self.loadingcharsindex = 0
Expand All @@ -36,7 +38,7 @@ def __init__(self,
# - 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):
while(number.value < max):
self.print(number.value,max)
time.sleep(float(updateperiod))
self.print(max,max)
Expand All @@ -47,47 +49,77 @@ def inThread(self, number, max, updateperiod=0.1):
# - number: progress value (int)
# - max: maximum value (int)
def print(self,number,max):
barstring = ""
actuallyjustifyright = True
consolewidth = os.get_terminal_size().columns

prebarstring = barstring = ""

# No carriage return on first print
if not self.firstprint:
barstring += "\r"
prebarstring += "\r"
self.firstprint = False

# Pre progress bar
### Pre progress bar
if self.pretext:
barstring += 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

# Progress bar
#
# Start char
if self.startendchar:
barstring += self.startendchar[0]

# Current state of affairs
sofarbar = int( (number/max)*self.barwidth )
remainingbar = self.barwidth - sofarbar
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
### Post progress bar
if self.displaypercentage:
barstring += " %d%%" % int(number*100/max)
per = " %d%%" % int(number*100/max)
barstring += " "*(5 - len(per)) + per
if self.displaycount:
barstring += " (%d/%d)" % (number,max)
count = " (%d/%d)" % (number,max)
barstring += " "*(len(str(max))-len(str(number))) + count

# Print the bar out
sys.stdout.write(barstring)
sys.stdout.flush()
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()

0 comments on commit c0dd11c

Please sign in to comment.