Initial directory restructuring to clarify dependencies
[be.git] / libbe / ui / util / pager.py
1 # Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
2 #
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.
7 #
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.
12 #
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.
16
17 """
18 Automatic pager for terminal output (a la Git).
19 """
20
21 import sys, os, select
22
23 # see http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
24 def run_pager(paginate='auto'):
25     """
26     paginate should be one of 'never', 'auto', or 'always'.
27
28     usage: just call this function and continue using sys.stdout like
29     you normally would.
30     """
31     if paginate == 'never' \
32             or sys.platform == 'win32' \
33             or not hasattr(sys.stdout, 'isatty') \
34             or sys.stdout.isatty() == False:
35         return
36
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']
44     else:
45         pager = 'less'
46
47     read_fd, write_fd = os.pipe()
48     if os.fork() == 0:
49         # child process
50         os.close(read_fd)
51         os.close(0)
52         os.dup2(write_fd, 1)
53         os.close(write_fd)
54         if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() == True:
55             os.dup2(1, 2)
56         return
57
58     # parent process, become pager
59     os.close(write_fd)
60     os.dup2(read_fd, 0)
61     os.close(read_fd)
62
63     # Wait until we have input before we start the pager
64     select.select([0], [], [])
65     os.execlp(pager, pager)