Skip to content

Instantly share code, notes, and snippets.

@heffcodex
Created November 7, 2018 21:42
Show Gist options
  • Save heffcodex/e245fb790ccfda3bd4bcb95ee292d79d to your computer and use it in GitHub Desktop.
Save heffcodex/e245fb790ccfda3bd4bcb95ee292d79d to your computer and use it in GitHub Desktop.
import sys
class TxtProgress:
_tpl = ""
def __init__(self, template):
self._tpl = template
def _syswrite(self, string):
sys.stdout.write(string)
sys.stdout.flush()
def write(self, *args):
self._syswrite("\r"+(self._tpl % args))
def cleanup(self):
self._syswrite("\r\n")
progress = TxtProgress("Test: %d -> %d") # define format
for i in range(1, 100):
progress.write(i, i*i) # update this string corresponding format arguments (like "Test: %d -> %d" % (i, i*i))
progress.cleanup() # normal output will be broken without it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment