Skip to content

Instantly share code, notes, and snippets.

@connorjak
Created October 4, 2018 05:01
Show Gist options
  • Save connorjak/d76571f2e500ea5b9e2b4830ea807d27 to your computer and use it in GitHub Desktop.
Save connorjak/d76571f2e500ea5b9e2b4830ea807d27 to your computer and use it in GitHub Desktop.
Variable-length progress bar with percent complete and activity visualizer.
# VisProgBar 1.0
#
# Copyright 2018 Connor Jakubik
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class VisProgBar:
"""Variable-length progress bar with percent complete and activity visualizer.
The progress bar has dithered intermediate character states for smooth
bar incrementing. The activity visualizer iterates every time the printBar
function is called, and is useful for gauging progress rate as well as
performance reduction from updating the bar too often."""
# ASCII characters for customization purposes:
# ▄█░▒▓├─┤┌└│-=≡¯‗
# ╔════════╗
# ║████▓───║
# ╚════════╝
def __init__(self):
self.barLength = 50
self.barText = ' BAR DID NOT GENERATE '
self.fractionComplete = 0.0
self.partialBar = 0.0
self.barsFilled = 0
self.barAmount = 0.0
self.printIterator = 0 #activity indicator iterator
self.activityDir = 1 #which way the activity indicator goes
self.activityLength = 15
self.postMessage = '' #displays after
def setLength(self,newLength):
"""Set the amount of segments (characters) inside the progress bar."""
self.barLength = newLength
def setActivityLength(self,newLength):
"""Set the amount of segments (characters) inside the activity visual.
Args:
- newLength (integer)"""
self.activityLength = newLength
def setFractionComplete(self,newCompleteness):
"""Set the fraction of progress in a 0.0->1.0 decimal float.
Args:
- newCompleteness (float)"""
self.fractionComplete = newCompleteness
def printBar(self, clearLine=True, noVis = False):
"""Print out the resultant progress bar and visualizer.
Optional Args:
-clearLine (boolean, default = True)
--- If true, end with carriage return instead of newline when
--- printing in order to overwrite on next print.
- noVis (boolean, default = False)
--- If true, don't print the visualizer."""
# *** Progress Bar ***
self.barText = ' ║'
self.barAmount = self.fractionComplete*self.barLength
self.barsFilled = math.floor(self.barAmount)
self.partialBar = self.barAmount-self.barsFilled
i = 0
# *** Full bars ***
while(i<self.barsFilled):
self.barText+='█'
i = i+1
# *** Partial - Half bar ***
#if(self.partialBar>0.5):
# self.barText+='▄'
# i = i+1
# *** Partial - Dithered Density ***
if(self.partialBar>0.75):
self.barText+='▓'
i = i+1
elif(self.partialBar>0.50):
self.barText+='▒'
i = i+1
elif(self.partialBar>0.25):
self.barText+='░'
i = i+1
# *** Empty bars ***
while(i<self.barLength):
self.barText+='‗'#-=≡¯‗
i = i+1
# *** End characters ***
self.barText+='║ '
if(self.fractionComplete<0.1):
self.barText+=' ' #formatting for percents under 10
self.barText+=truncate(self.fractionComplete*100,2) + '%'
# *** Activity indicator ***
if(~noVis):
self.barText+=' ║'
j = 0
while(j<self.printIterator):
self.barText+=' '
j+=1
self.barText+='■'#■¤
j+=1
while(j<self.activityLength):
self.barText+=' '
j+=1
self.barText+='║'
self.printIterator += self.activityDir #iterate motion in either direction
if(self.printIterator>=self.activityLength-1):
self.activityDir = -self.activityDir #flip iteration direction
elif(self.printIterator<=0):
self.activityDir = -self.activityDir #flip iteration direction
# *** Post-message ***
self.barText+=self.postMessage
# *** Printing ***
if(clearLine):
print(self.barText, end='\r')
else:
print(self.barText, end='\n')
def printComplete(self, clearLine=False):
"""Print out the progress bar at 100%. This avoids issues with ending at
99.99% due to floating point roundoff.
Optional Args:
- clearLine (boolean, default = True)
--- If true, end with carriage return instead of newline when
--- printing in order to overwrite on next print."""
# *** Progress Bar ***
self.barText = ' ║'
i = 0
while(i<self.barLength):
self.barText+='█'
i = i+1
self.barText+='║'
self.barText+=' ' + '100.00' + '%'
# *** Post-message ***
self.barText+=self.postMessage
# *** Erase Previously Printed ***
j = 0
end = self.activityLength + 8
while(j<end):
self.barText+=' '
j+=1
# *** Printing ***
if(clearLine):
print(self.barText, end='\r')
else:
print(self.barText, end='\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment