[PATCH] Use the pager given by the environment to display long output

David Soria Parra sn_ at gmx.net
Tue Mar 18 16:32:04 CDT 2008


Bryan O'Sullivan wrote:
> David Soria Parra wrote:
>> I tried to follow all advices from the people who replied to the last thread about paging.
> 
> Good stuff, thanks.  Still one last comment:
> 
>> +            if self.getpager() and not self.pager:
>> +                self.pager = os.popen(self.getpager(), "w")
>>              for a in args:
>> -                sys.stdout.write(str(a))
>> +                if self.pager:
>> +                    self.pager.write(a)
>> +                else:
>> +                    sys.stdout.write(str(a))
> 
> I think that it would make more sense to set sys.stdout (and probably
> sys.stderr) to the popened value, because this will avoid the need for
> self.pager and will keep the code changes more local.
> 

I totally agree.

Here is the updated patch. I decided not to drop the self.pager property at all as without that the
check in __del__ get's pretty nasty.

                                  david

----

# HG changeset patch
# User David Soria Parra <dsp at php.net>
# Date 1205874754 -3600
# Node ID 228a02fb9f725f1a47e4f70304453a8880c76768
# Parent  c50ac875ffcb8ae53beeeeee119a4dae677c4d23
Use the pager given by the environment to display long output

Unix systems usually have a PAGER environment variable set.
If it is set, mercurial will use the pager application to display
output.

Two configuration variables are available to influence the behaviour of the
pager. ui.pager sets the pager application. The pager is
only used if ui.usepager is true. By default ui.usepager is disabled.

diff -r c50ac875ffcb -r 228a02fb9f72 doc/hgrc.5.txt
--- a/doc/hgrc.5.txt	Sat Mar 15 16:51:53 2008 -0500
+++ b/doc/hgrc.5.txt	Tue Mar 18 22:12:34 2008 +0100
@@ -522,6 +522,12 @@
     Print debugging information.  True or False.  Default is False.
   editor;;
     The editor to use during a commit.  Default is $EDITOR or "vi".
+  pager;;
+    The pager that is used when displaying long output.
+    Default is $PAGER. If not set, the output is written to the
+    stdandard output.
+  usepager;;
+    If set to true, the system pager is used. True or False. Default is False.
   fallbackencoding;;
     Encoding to try if it's not possible to decode the changelog using
     UTF-8.  Default is ISO-8859-1.
diff -r c50ac875ffcb -r 228a02fb9f72 mercurial/ui.py
--- a/mercurial/ui.py	Sat Mar 15 16:51:53 2008 -0500
+++ b/mercurial/ui.py	Tue Mar 18 22:12:34 2008 +0100
@@ -31,6 +31,7 @@
                  parentui=None):
         self.overlay = None
         self.buffers = []
+        self.pager = None
         if parentui is None:
             # this is the parent of all ui children
             self.parentui = None
@@ -63,6 +64,15 @@

     def __getattr__(self, key):
         return getattr(self.parentui, key)
+
+    def __del__(self):
+        if self.pager:
+            try:
+                self.pager.close()
+            except IOException:
+                # we might get into an broken pipe if the users quit
+                # the pager before we finished io
+                pass

     def isatty(self):
         if ui._isatty is None:
@@ -370,9 +380,14 @@
         return "".join(self.buffers.pop())

     def write(self, *args):
+        """Write to a pager if available, otherwise to stdout"""
         if self.buffers:
             self.buffers[-1].extend([str(a) for a in args])
         else:
+            if self.getpager() and not self.pager:
+                self.pager = os.popen(self.getpager(), "w")
+                sys.stderr = self.pager
+                sys.stdout = self.pager
             for a in args:
                 sys.stdout.write(str(a))

@@ -478,3 +493,8 @@
                 os.environ.get("VISUAL") or
                 os.environ.get("EDITOR", "vi"))

+    def getpager(self):
+        '''return a pager'''
+        if self.configbool("ui", "usepager", False):
+            return (self.config("ui", "pager")
+                    or os.environ.get("PAGER"))


More information about the Mercurial-devel mailing list