""" display an ascii barGraph within the boundary of a terminal console. This is just an experiment for learning and do to some practicing on python3. code is free for non-commercial personal and educational use. Author: Axel Werner [axel.werner.1973@gmail.com] Version: 2015-08-10 Personal Comments and ToDos: ============================= - i know this code is not very beautiful yet and is very cluttered. if i would reuse it, i should convert it into a class and do more re-factoring to make it friendlier to read and easy to maintain. """ import shutil import random import time consoleWidth, consoleHeight = shutil.get_terminal_size() # returns a integer tuple leftDelimiter="[" rightDelimiter="]" progressBarChar="=" progressBarFillerChar="." limiterLastRandNum=0.5 # global def getProgressBar( progressPercentage=0.0, width=50 ): """ return progressBar as string ranging from float 0.0 to 1.0 .""" if progressPercentage > 1: print("ERROR: percentage must be between 0.0 and 1.0") print(" instead the percentage i received is:", progressPercentage) exit(1) percentageDisplayStr= str(int(progressPercentage*100))+"%" LengthOfpercentageDisplayStr=len(percentageDisplayStr) maxWidthOfpercentageDisplayStr=4 # '100%' numOfFillersNeeded=maxWidthOfpercentageDisplayStr-LengthOfpercentageDisplayStr percentageDisplayStr=percentageDisplayStr+numOfFillersNeeded*" " maxBarWidth=width-1-( len(leftDelimiter)+len(rightDelimiter)+len(percentageDisplayStr) ) effectiveWidthOfProgressBar=int(maxBarWidth*progressPercentage) effectiveWidthOfFillerBar=int(maxBarWidth-effectiveWidthOfProgressBar) progressString=effectiveWidthOfProgressBar*progressBarChar fillerString=effectiveWidthOfFillerBar*progressBarFillerChar wholeBarStr=percentageDisplayStr+leftDelimiter+progressString+fillerString+rightDelimiter return wholeBarStr def limitedRandGen(): """ return a random Number between 0.0 and 1.0 within limited dynamic range of 3%. """ global limiterLastRandNum newRandNum=random.random() delta=abs(limiterLastRandNum-newRandNum) # Betrag bestimmen (Vorzeichen ignorieren) while (delta*100) > 3.0 : # set limitation percentage here newRandNum=random.random() delta=abs(limiterLastRandNum-newRandNum) # Betrag bestimmen (Vorzeichen ignorieren) limiterLastRandNum=newRandNum return newRandNum print("\n"*3) print("DEMO1: run bargraph straight from 0 to 100%") for progress in range(100+1): print( getProgressBar(progress/100.0, width=consoleWidth) ) print("\n"*3) print("DEMO2: displaying 300 random percentages barGraphs on the same line at full width") numOfFrames=300 for i in range(numOfFrames): print( "\r" + getProgressBar( progressPercentage=limitedRandGen(), width=consoleWidth ), end="" ) time.sleep(50 / 1000) # delays for x seconds print("\n"*3) print("DEMO3: displaying 300 random percentages barGraphs on the same line at width=50") numOfFrames=300 for i in range(numOfFrames): print( "\r" + getProgressBar( progressPercentage=limitedRandGen(), width=50 ), end="" ) time.sleep(50 / 1000) # delays for x seconds print("\n"*3) print("consoleWidth =", consoleWidth) input("press ENTER to continue...")