Created
July 22, 2012 22:07
-
-
Save Midnighter/3161164 to your computer and use it in GitHub Desktop.
Dynamic printing of recurring information to a single line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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