Skip to content

Instantly share code, notes, and snippets.

@Nooby
Created August 7, 2011 15:59
Show Gist options
  • Save Nooby/1130486 to your computer and use it in GitHub Desktop.
Save Nooby/1130486 to your computer and use it in GitHub Desktop.
Simple Progress Bar for Console Applications
#!/usr/bin/python
import sys
import StringIO
class SimpleProgress():
def __init__(self, message, max):
self.message = message
self.max = max
self.current = 0
self.out = sys.stdout
self.stringIoBuffer = StringIO.StringIO()
sys.stdout = self.stringIoBuffer
self.reprint()
def __del__(self):
self.out.write("\r" + " "*40 + "\r") #TODO: Refactor
self.out.write(self.stringIoBuffer.getvalue())
sys.stdout = self.out
def step(self):
self.current += 1
self.reprint()
def reprint(self):
self.out.write("\r{0} {1} of {2}".format(self.message, self.current, self.max))
if __name__ == "__main__":
import time
r = 10
progress = SimpleProgress("Progress", r)
for i in range(r):
progress.step()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment