1 # Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org>
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Bugs Everywhere.
6 # Bugs Everywhere is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation, either version 2 of the License, or (at your
9 # option) any later version.
11 # Bugs Everywhere is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Bugs Everywhere. If not, see <http://www.gnu.org/licenses/>.
20 Automatic pager for terminal output (a la Git).
23 import sys, os, select
25 # see http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
26 def run_pager(paginate='auto'):
28 paginate should be one of 'never', 'auto', or 'always'.
30 usage: just call this function and continue using sys.stdout like
33 if paginate == 'never' \
34 or sys.platform == 'win32' \
35 or not hasattr(sys.stdout, 'isatty') \
36 or sys.stdout.isatty() == False:
39 if paginate == 'auto':
40 if 'LESS' not in os.environ:
41 os.environ['LESS'] = '' # += doesn't work on undefined var
42 # don't page if the input is short enough
43 os.environ['LESS'] += ' -FRX'
44 if 'PAGER' in os.environ:
45 pager = os.environ['PAGER']
49 read_fd, write_fd = os.pipe()
56 if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() == True:
60 # parent process, become pager
65 # Wait until we have input before we start the pager
66 select.select([0], [], [])
67 os.execlp(pager, pager)