Commits

Eduard-Cristian Stefan committed 47d02ab

cleanup

  • Participants
  • Parent commits f26ac2a

Comments (0)

Files changed (1)

 # tpager.py - browse command output with an external pager using temp files
 #
 # Copyright (C) 2008 David Soria Parra <dsp@php.net>
-# Copyright (C) 2010 Eduard Stefan <alexandrul.ct@gmail.com>
+# Copyright (C) 2010-2012 Eduard-Cristian Stefan <alexandrul.ct@gmail.com>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 
-'''browse command output with an external pager using temp files
+"""browse command output with an external pager using temp files
 
 This extension is based on the original Mercurial pager extension,
 and uses the same format for the configuration section.
 
   [pager]
   hg16mode = yes
-'''
+"""
 
 
 import os
-import signal
 import subprocess
 import sys
 import tempfile
 
-from mercurial import dispatch, util, extensions
+from mercurial import dispatch, extensions, util
 
 
 def uisetup(ui):
 
+    if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout):
+        return
+
     def pagecmd(orig, ui, options, cmd, cmdfunc):
 
-        # check environment
-        if (('HGPLAIN' in os.environ) or
-            (ui.plain()) or
-            ('--debugger' in sys.argv)):
+        pager = ui.config('pager', 'pager', os.environ.get('PAGER'))
+        pageroptions = ui.config('pager', 'pageroptions', '')
+        hg16mode = ui.config('pager', 'hg16mode', 'YES').upper()
+
+        if not (pager and (cmd in ui.configlist('pager', 'attend', attended))):
             return orig(ui, options, cmd, cmdfunc)
 
-        # check for tty
-        t = False
-
-        try:
-            if sys.stdout.isatty():
-                t = True
-        except AttributeError:
-            t = False
-
-        if not t:
-            return orig(ui, options, cmd, cmdfunc)
-
-        # check Mercurial 1.6+ mode
-        hg16mode = ui.config('pager', 'hg16mode', 'no')
-
-        # check for pager app and valid command
-        p = ui.config('pager', 'pager', os.environ.get('PAGER'))
-
-        if ((not p) or
-            (cmd not in ui.configlist('pager', 'attend', attended))):
-            return orig(ui, options, cmd, cmdfunc)
-
-        # running pager
+        ui.setconfig('ui', 'formatted', ui.formatted())
         ui.setconfig('ui', 'interactive', False)
 
         ui.pushbuffer()
+
         exitcode = orig(ui, options, cmd, cmdfunc)
 
-        if hg16mode == 'yes':
-            pcontent = ui.popbuffer(True)
+        if hg16mode == 'YES':
+            content = ui.popbuffer(True)
         else:
-            pcontent = ui.popbuffer()
+            content = ui.popbuffer()
 
-        fname = tempfile.NamedTemporaryFile().name
+        tmpfile = tempfile.NamedTemporaryFile(delete=False).name
+        f = open(tmpfile, 'wb')
 
-        f = open(fname, 'wb')
-
-        # strip CR
-        for line in pcontent:
-            f.write(line.replace("\r", ""))
+        for line in content:
+            f.write(line)
 
         f.flush()
         f.close()
 
-        subprocess.call(p + " " + fname)
+        callparams = [pager, tmpfile]
+        if pageroptions:
+            callparams.insert(1, pageroptions)
 
-        os.remove(fname)
+        subprocess.Popen(callparams).wait()
+
+        os.remove(tmpfile)
 
         return exitcode
 
     extensions.wrapfunction(dispatch, '_runcommand', pagecmd)
 
-attended = ['annotate', 'bookmarks', 'cat', 'diff', 'export', 'glog', 'heads', 'help', 'log', 'parents', 'qdiff', 'status', 'summary', 'tip']
+
+attended = ['annotate',
+            'branch',
+            'branches',
+            'bookmarks',
+            'cat',
+            'diff',
+            'diffstat',
+            'export',
+            'glog',
+            'heads',
+            'help',
+            'log',
+            'parents',
+            'qdiff',
+            'status',
+            'summary',
+            'tags',
+            'tip']