Skip to content

Instantly share code, notes, and snippets.

@jsmits
Created May 3, 2009 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmits/105880 to your computer and use it in GitHub Desktop.
Save jsmits/105880 to your computer and use it in GitHub Desktop.
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