diff options
author | W. Trevor King <wking@drexel.edu> | 2009-12-07 07:36:14 -0500 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2009-12-07 07:36:14 -0500 |
commit | b7901b5054a4bf51798098cd3aaa3eb33bb4ee5e (patch) | |
tree | 3a1a746945d2fcad3cf67abd5b0b136ede81588f | |
parent | fdf9925ffaada614544d1b2d3ccecb42f1549acb (diff) | |
parent | a2562bc912e33fb3748be9d01771c9ae0ed6010f (diff) | |
download | bugseverywhere-b7901b5054a4bf51798098cd3aaa3eb33bb4ee5e.tar.gz |
Added libbe.pager and --paginate/--no-pager options to be.
-rw-r--r-- | NEWS | 3 | ||||
-rwxr-xr-x | be | 16 | ||||
-rw-r--r-- | libbe/pager.py | 65 |
3 files changed, 82 insertions, 2 deletions
@@ -1,4 +1,5 @@ -December 6, 2009 +December 7, 2009 + * added --paginate and --no-pager to be. * be --dir DIR COMMAND now roots the bugdir in DIR _without_ changing directories. * `be init --root DIR` should now be `be --dir DIR init`. @@ -22,7 +22,7 @@ import os import sys -from libbe import cmdutil, version +from libbe import cmdutil, version, pager __doc__ = cmdutil.help() @@ -36,6 +36,13 @@ parser.add_option("--verbose-version", action="store_true", dest="verbose_versio help="Print verbose version information and exit.") parser.add_option("-d", "--dir", dest="dir", metavar="DIR", default=".", help="Run this command on the repository in DIR instead of the current directory.") +parser.add_option("-p", "--paginate", dest="paginate", default=False, + action='store_true', + help="Pipe all output into less (or if set, $PAGER).") +parser.add_option("--no-pager", dest="no_pager", default=False, + action='store_true', + help="Do not pipe git output into a pager.") + try: options,args = parser.parse_args() @@ -57,6 +64,13 @@ if options.version == True or options.verbose_version == True: print version.version(verbose=options.verbose_version) sys.exit(0) +paginate = 'auto' +if options.paginate == True: + paginate = 'always' +if options.no_pager== True: + paginate = 'never' +pager.run_pager(paginate) + try: if len(args) == 0: raise cmdutil.UsageError, "must supply a command" diff --git a/libbe/pager.py b/libbe/pager.py new file mode 100644 index 0000000..1ddc3fa --- /dev/null +++ b/libbe/pager.py @@ -0,0 +1,65 @@ +# Copyright (C) 2009 W. Trevor King <wking@drexel.edu> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" +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='auto'): + """ + 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) |