diff options
author | W. Trevor King <wking@drexel.edu> | 2009-12-07 07:00:34 -0500 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2009-12-07 07:00:34 -0500 |
commit | adfd866bf1fafc3832e03506a09c9b5750fe7447 (patch) | |
tree | 007dcfa2cd7a26257f4749d0c2b857b1d9c1fb91 /libbe | |
parent | ba31b657c49649ee0b00663a32e907bb482270ac (diff) | |
download | bugseverywhere-adfd866bf1fafc3832e03506a09c9b5750fe7447.tar.gz |
Added libbe.pager
Diffstat (limited to 'libbe')
-rw-r--r-- | libbe/pager.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libbe/pager.py b/libbe/pager.py new file mode 100644 index 0000000..8190b2a --- /dev/null +++ b/libbe/pager.py @@ -0,0 +1,51 @@ +# Copyright + +""" +Automatic pager for terminal output (a la Git). +""" + +import sys, os, select + +# see http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby +def run_pager(paginate=None): + """ + paginate should be one of 'never', 'auto', or 'always'. + + usage: just call this function and continue using sys.stdout like + you normally would. + """ + if paginate == 'never' \ + or sys.platform == 'win32' \ + or not hasattr(sys.stdout, 'isatty') \ + or sys.stdout.isatty() == False: + return + + if paginate == 'auto': + if 'LESS' not in os.environ: + os.environ['LESS'] = '' # += doesn't work on undefined var + # don't page if the input is short enough + os.environ['LESS'] += ' -FRX' + if 'PAGER' in os.environ: + pager = os.environ['PAGER'] + else: + pager = 'less' + + read_fd, write_fd = os.pipe() + if os.fork() == 0: + # child process + os.close(read_fd) + os.close(0) + os.dup2(write_fd, 1) + os.close(write_fd) + if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() == True: + os.dup2(1, 2) + return + + # parent process, become pager + os.close(write_fd) + os.dup2(read_fd, 0) + os.close(read_fd) + + # Wait until we have input before we start the pager + select.select([0], [], []) + os.execlp(pager, pager) |