Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created July 22, 2012 22:07
Show Gist options
  • Save Midnighter/3161164 to your computer and use it in GitHub Desktop.
Save Midnighter/3161164 to your computer and use it in GitHub Desktop.
Dynamic printing of recurring information to a single line.
class ProgressPrinter(object):
"""
Write a recurring message to a stream on the same line.
Warning
-------
Only works properly if the message fits on a single line for the given
stream.
"""
def __init__(self, stream=sys.stdout, sep=" ", **kw_args):
super(ProgressPrinter, self).__init__(**kw_args)
self.stream = stream
self.sep = sep
def __call__(self, msg, *args):
self.stream.write("\r\x1b[K")
self.stream.write(msg)
if args:
self.stream.write(self.sep)
self.stream.write(self.sep.join(args))
self.stream.flush()
def close(self):
self.stream.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment