hgext/pager.py
branchstable
changeset 11182 3c368a1c962d
parent 10516 80a1161bc3b5
child 11186 a890cc501501
child 11240 ccb4057e19e6
equal deleted inserted replaced
11171:3b3261f6d9ba 11182:3c368a1c962d
    47 
    47 
    48 To ignore global commands like "hg version" or "hg help", you have to
    48 To ignore global commands like "hg version" or "hg help", you have to
    49 specify them in the global .hgrc
    49 specify them in the global .hgrc
    50 '''
    50 '''
    51 
    51 
    52 import sys, os, signal
    52 import sys, os, signal, shlex
    53 from mercurial import dispatch, util, extensions
    53 from mercurial import dispatch, util, extensions
       
    54 
       
    55 def _runpager(p):
       
    56     if not hasattr(os, 'fork'):
       
    57         sys.stderr = sys.stdout = util.popen(p, 'wb')
       
    58         return
       
    59     fdin, fdout = os.pipe()
       
    60     pid = os.fork()
       
    61     if pid == 0:
       
    62         os.close(fdin)
       
    63         os.dup2(fdout, sys.stdout.fileno())
       
    64         os.dup2(fdout, sys.stderr.fileno())
       
    65         os.close(fdout)
       
    66         return
       
    67     os.dup2(fdin, sys.stdin.fileno())
       
    68     os.close(fdin)
       
    69     os.close(fdout)
       
    70     args = shlex.split(p)
       
    71     os.execvp(args[0], args)
    54 
    72 
    55 def uisetup(ui):
    73 def uisetup(ui):
    56     def pagecmd(orig, ui, options, cmd, cmdfunc):
    74     def pagecmd(orig, ui, options, cmd, cmdfunc):
    57         p = ui.config("pager", "pager", os.environ.get("PAGER"))
    75         p = ui.config("pager", "pager", os.environ.get("PAGER"))
    58         if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
    76         if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
    59             attend = ui.configlist('pager', 'attend', attended)
    77             attend = ui.configlist('pager', 'attend', attended)
    60             if (cmd in attend or
    78             if (cmd in attend or
    61                 (cmd not in ui.configlist('pager', 'ignore') and not attend)):
    79                 (cmd not in ui.configlist('pager', 'ignore') and not attend)):
    62                 ui.setconfig('ui', 'interactive', False)
    80                 ui.setconfig('ui', 'interactive', False)
    63                 sys.stderr = sys.stdout = util.popen(p, "wb")
    81                 _runpager(p)
    64                 if ui.configbool('pager', 'quiet'):
    82                 if ui.configbool('pager', 'quiet'):
    65                     signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    83                     signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    66         return orig(ui, options, cmd, cmdfunc)
    84         return orig(ui, options, cmd, cmdfunc)
    67 
    85 
    68     extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
    86     extensions.wrapfunction(dispatch, '_runcommand', pagecmd)