Patch updated

David Soria Parra sn_ at gmx.net
Thu Mar 6 03:25:40 CST 2008


i updated the patch, so it only displays the pager when a minimum number
of lines is used, and I help now uses the pager.

As christian said, it's not python 2.3 compatible

# HG changeset patch
# User David Soria Parra <sn_ at gmx.net>
# Date 1204795366 -3600
# Node ID e38e2f5a22fcfe52548124d0a0e5ad64ff54495f
# Parent  0068809347d72e552f980487ee669ace9a82d214
Add a pager to display long output using the pager given by the environment

Long output like annotate, help, log or diff are now using the given
pager from
the environment when the output is longer than a default value (default:
25 lines)
to display the content.

diff -r 0068809347d7 -r e38e2f5a22fc mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Fri Feb 29 14:48:21 2008 -0800
+++ b/mercurial/cmdutil.py	Thu Mar 06 10:22:46 2008 +0100
@@ -9,6 +9,7 @@ from i18n import _
 from i18n import _
 import os, sys, bisect, stat
 import mdiff, bdiff, util, templater, templatefilters, patch, errno
+import ui.pager as pager

 revrangesep = ':'

@@ -558,6 +559,8 @@ class changeset_printer(object):
     '''show changeset information when templating not requested.'''

     def __init__(self, ui, repo, patch, buffered):
+        self.pager = pager(ui)
+
         self.ui = ui
         self.repo = repo
         self.buffered = buffered
@@ -571,10 +574,10 @@ class changeset_printer(object):
             h = self.header[rev]
             if h != self.lastheader:
                 self.lastheader = h
-                self.ui.write(h)
+                self.pager.write(h)
             del self.header[rev]
         if rev in self.hunk:
-            self.ui.write(self.hunk[rev])
+            self.pager.write(self.hunk[rev])
             del self.hunk[rev]
             return 1
         return 0
diff -r 0068809347d7 -r e38e2f5a22fc mercurial/commands.py
--- a/mercurial/commands.py	Fri Feb 29 14:48:21 2008 -0800
+++ b/mercurial/commands.py	Thu Mar 06 10:22:46 2008 +0100
@@ -12,6 +12,7 @@ import difflib, patch, time, help, mdiff
 import difflib, patch, time, help, mdiff, tempfile
 import errno, version, socket
 import archival, changegroup, cmdutil, hgweb.server, sshserver, hbisect
+import ui.pager as pager

 # Commands start here, listed alphabetically

@@ -919,12 +920,17 @@ def diff(ui, repo, *pats, **opts):
     it detects as binary. With -a, diff will generate a diff anyway,
     probably with undesirable results.
     """
+    ui = pager(ui)
+    repo.ui = ui
+
     node1, node2 = cmdutil.revpair(repo, opts['rev'])
+

     fns, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)

     patch.diff(repo, node1, node2, fns, match=matchfn,
                opts=patch.diffopts(ui, opts))
+

 def export(ui, repo, *changesets, **opts):
     """dump the header and diffs for one or more changesets
@@ -1193,6 +1199,7 @@ def help_(ui, name=None, with_version=Fa
     commands it provides."""
     option_lists = []

+    ui = pager(ui)
     def addglobalopts(aliases):
         if ui.verbose:
             option_lists.append((_("global options:"), globalopts))
@@ -1226,6 +1233,7 @@ def help_(ui, name=None, with_version=Fa
         doc = i[0].__doc__
         if not doc:
             doc = _("(No help text available)")
+
         if ui.quiet:
             doc = doc.splitlines(0)[0]
         ui.write("\n%s\n" % doc.rstrip())
diff -r 0068809347d7 -r e38e2f5a22fc mercurial/ui.py
--- a/mercurial/ui.py	Fri Feb 29 14:48:21 2008 -0800
+++ b/mercurial/ui.py	Thu Mar 06 10:22:46 2008 +0100
@@ -1,13 +1,14 @@
 # ui.py - user interface bits for mercurial
 #
 # Copyright 2005-2007 Matt Mackall <mpm at selenic.com>
+# Copyright 2008 David Soria Parra <sn_ at gmx.net>
 #
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.

 from i18n import _
 import errno, getpass, os, re, socket, sys, tempfile
-import ConfigParser, traceback, util
+import ConfigParser, traceback, util, subprocess

 def dupconfig(orig):
     new = util.configparser(orig.defaults())
@@ -478,3 +479,50 @@ class ui(object):
                 os.environ.get("VISUAL") or
                 os.environ.get("EDITOR", "vi"))

+class pager(object):
+    """
+    Use the environment pager that is usually set on
+    UNIX like systems to display the contents of e.g. the reflog
+
+    the pager is not used if the output is not bigger than min_lines
+    but instead just print to stdout.
+    if min_lines is 0, the pager is always used
+    """
+    def __init__(self, ui, min_lines=25):
+        self.buffer = []
+        self.ui = ui
+        self.min_lines = min_lines
+
+    def __del__(self):
+        if len(self.buffer) < self.min_lines:
+            self.ui.write(os.linesep.join(self.buffer) + os.linesep)
+        elif self.proc:
+            self.proc.communicate()
+
+    def __getattr__(self, key):
+        try:
+            return getattr(self.ui, key)
+        except AttributeError:
+            pass
+
+    def write(self, w):
+        self.buffer.extend(w.splitlines())
+        if len(self.buffer) >= self.min_lines and not self.buffer_written:
+            # we reached the limit and not yet written the buffer
+            self.buffer_written = True
+            if self.getpager() and not self.proc:
+                # we now that we need a process only if we already
reach the lines_to_page limit
+                self.proc = subprocess.Popen(self.getpager(),
stdin=subprocess.PIPE)
+                self.proc.stdin.write(os.linesep.join(self.buffer))
+            else:
+                self.ui.write(os.linesep.join(self.buffer))
+        elif self.buffer_written:
+            if self.proc:
+                self.proc.stdin.write(w)
+            else:
+                self.ui.write(w)
+
+    def getpager(self):
+        '''return a pager'''
+        return (os.environ.get("PAGER") or
+                self.config("ui", "pager"))



More information about the Mercurial-devel mailing list