[PATCH STABLE, RFC] ui: add a formatted() method -- like interactive(), but for output

Dan Villiom Podlaski Christiansen danchr at gmail.com
Tue May 4 16:10:29 CDT 2010


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1273007228 -7200
# Branch stable
# Node ID 5b66df12549a2c01c603c12151689a945037a542
# Parent  69145d9b4127d3307a99d19e3321bead0058f3a5
ui: add a formatted() method -- like interactive(), but for output

This patch adds a new method to UI instances, `ui.formatted()', for
non-interactive sessions where formatting should be allowed. The
`pager' is updated to set this. The `mq' extensions is updated to use
it. The fact that is implemented using a `ui.formatted' configuration
variable should be considered an implementation detail.

The intended, eventual result of this is:

1) Piping the result of Mercurial to another process disables *all*
   terminal formatting and adaptation.
2) Using the pager extension retains *all* formatting that would
   otherwise have been done.

I haven't tested this patch extensively; it is primarily provided for
testing & comment. Eventually, the progress extension should also use
this infrastructure.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -337,7 +337,7 @@ def _setupcmd(ui, cmd, table, func, effe
 
         if (opts['no_color'] or opts['color'] == 'never' or
             (opts['color'] == 'auto' and (os.environ.get('TERM') == 'dumb'
-                                          or not sys.__stdout__.isatty()))):
+                                          or not ui.formatted()))):
             del opts['no_color']
             del opts['color']
             return orig(*args, **opts)
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -1430,7 +1430,7 @@ class queue(object):
             if summary:
                 ph = patchheader(self.join(patchname), self.plainmode)
                 msg = ph.message and ph.message[0] or ''
-                if not self.ui.plain():
+                if self.ui.formatted():
                     width = util.termwidth() - len(pfx) - len(patchname) - 2
                     if width > 0:
                         msg = util.ellipsis(msg, width)
diff --git a/hgext/pager.py b/hgext/pager.py
--- a/hgext/pager.py
+++ b/hgext/pager.py
@@ -59,6 +59,7 @@ def uisetup(ui):
             attend = ui.configlist('pager', 'attend', attended)
             if (cmd in attend or
                 (cmd not in ui.configlist('pager', 'ignore') and not attend)):
+                ui.setconfig('ui', 'formatted', ui.formatted())
                 ui.setconfig('ui', 'interactive', False)
                 sys.stderr = sys.stdout = util.popen(p, "wb")
                 if ui.configbool('pager', 'quiet'):
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -281,6 +281,21 @@ class ui(object):
 
         return i
 
+    def formatted(self):
+        if self.plain():
+            return False
+
+        i = self.configbool("ui", "formatted", None)
+        if i is None:
+            try:
+                return sys.stdout.isatty()
+            except AttributeError:
+                # some environments replace stdout without implementing isatty
+                # usually those are non-interactive
+                return False
+
+        return i
+
     def _readline(self, prompt=''):
         if sys.stdin.isatty():
             try:


More information about the Mercurial-devel mailing list