1 # Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 Automatic pager for terminal output (a la Git).
21 import sys, os, select
23 # see http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
24 def run_pager(paginate='auto'):
26 paginate should be one of 'never', 'auto', or 'always'.
28 usage: just call this function and continue using sys.stdout like
31 if paginate == 'never' \
32 or sys.platform == 'win32' \
33 or not hasattr(sys.stdout, 'isatty') \
34 or sys.stdout.isatty() == False:
37 if paginate == 'auto':
38 if 'LESS' not in os.environ:
39 os.environ['LESS'] = '' # += doesn't work on undefined var
40 # don't page if the input is short enough
41 os.environ['LESS'] += ' -FRX'
42 if 'PAGER' in os.environ:
43 pager = os.environ['PAGER']
47 read_fd, write_fd = os.pipe()
54 if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() == True:
58 # parent process, become pager
63 # Wait until we have input before we start the pager
64 select.select([0], [], [])
65 os.execlp(pager, pager)