Created
May 3, 2009 06:50
-
-
Save jsmits/105880 to your computer and use it in GitHub Desktop.
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
import sys | |
import subprocess | |
class Pager(object): | |
"""enable paging for multiple writes""" | |
def __init__(self): | |
self.proc = None | |
if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): | |
pager = 'morelli -EMR' # wrong command, on purpose | |
# pager = 'more -EMR' | |
self.proc = subprocess.Popen([pager], shell=True, | |
stdin=subprocess.PIPE, stderr=subprocess.PIPE) | |
self.file = self.proc.stdin | |
else: | |
self.file = sys.stdout | |
def write(self, text=""): | |
try: | |
self.file.write("%s\n" % text) | |
except: | |
# in case the pager is not available on the system, for example | |
self.file = sys.stdout | |
self.write(text) | |
def close(self): | |
if self.proc: | |
self.file.close() | |
self.proc.wait() | |
if __name__ == '__main__': | |
printer = Pager() | |
printer.write("this is the header") | |
printer.write("this is the first line") | |
printer.write("this is the body") | |
printer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment