Use the pager given by the environment to display long output
authorDavid Soria Parra <dsp@php.net>
Tue, 18 Mar 2008 22:12:34 +0100
changeset 6302 8e3dc3de7e73
parent 6301 68cfd7d208a5
child 6303 577a7da1d44b
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.
doc/hgrc.5.txt
mercurial/ui.py
--- a/doc/hgrc.5.txt	Tue Mar 18 18:14:59 2008 -0300
+++ 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.
--- a/mercurial/ui.py	Tue Mar 18 18:14:59 2008 -0300
+++ 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
@@ -64,6 +65,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:
             ui._isatty = sys.stdin.isatty()
@@ -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"))