[PATCH] Add ui.width configuration variable

Alexander Solovyov piranha at piranha.org.ua
Sat Aug 1 09:03:20 CDT 2009


# HG changeset patch
# User Alexander Solovyov <piranha at piranha.org.ua>
# Date 1249135312 -10800
# Node ID 565507a27d6b7a96be62493d0c3be87075879fb5
# Parent  45aaf27f95b8243fcfcd056fbc9bd4cfd2525dbe
Add ui.width configuration variable

This allows user to define wrapping column for help without using COLUMNS
environment variable (which will have effect on other tools).

diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt
--- a/doc/hgrc.5.txt
+++ b/doc/hgrc.5.txt
@@ -799,6 +799,8 @@ merge-tools section.
     "username =" in the system hgrc).
 ``verbose``
     Increase the amount of output printed. True or False. Default is False.
+``width``
+    Width to which all generated output will be wrapped.
 
 
 ``web``
diff --git a/hgext/churn.py b/hgext/churn.py
--- a/hgext/churn.py
+++ b/hgext/churn.py
@@ -149,7 +149,7 @@ def churn(ui, repo, *pats, **opts):
     maxcount = float(max([v for k, v in rate]))
     maxname = max([len(k) for k, v in rate])
 
-    ttywidth = util.termwidth()
+    ttywidth = ui.termwidth()
     ui.debug(_("assuming %i character terminal\n") % ttywidth)
     width = ttywidth - maxname - 2 - 6 - 2 - 2
 
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -1355,7 +1355,7 @@ class queue(object):
                 msg = ''
             msg = "%s%s%s" % (pfx, patchname, msg)
             if self.ui.interactive():
-                msg = util.ellipsis(msg, util.termwidth())
+                msg = util.ellipsis(msg, self.ui.termwidth())
             self.ui.write(msg + '\n')
 
         applied = set([p.name for p in self.applied])
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1423,7 +1423,7 @@ def help_(ui, name=None, with_version=Fa
     Given a topic, extension, or command name, print help for that
     topic."""
     option_lists = []
-    textwidth = util.termwidth() - 2
+    textwidth = ui.termwidth() - 2
 
     def addglobalopts(aliases):
         if ui.verbose:
@@ -1521,11 +1521,12 @@ def help_(ui, name=None, with_version=Fa
                 commands = cmds[f].replace("|",", ")
                 ui.write(" %s:\n      %s\n"%(commands, h[f]))
             else:
-                ui.write(' %-*s   %s\n' % (m, f, util.wrap(h[f], m + 4)))
+                ui.write(' %-*s   %s\n' %
+                         (m, f, util.wrap(h[f], m + 4, ui.termwidth())))
 
         if name != 'shortlist':
             exts, maxlength = extensions.enabled()
-            text = help.listexts(_('enabled extensions:'), exts, maxlength)
+            text = help.listexts(ui, _('enabled extensions:'), exts, maxlength)
             if text:
                 ui.write("\n%s\n" % minirst.format(text, textwidth))
 
@@ -1628,7 +1629,7 @@ def help_(ui, name=None, with_version=Fa
         opts_len = max([len(line[0]) for line in opt_output if line[1]] or [0])
         for first, second in opt_output:
             if second:
-                second = util.wrap(second, opts_len + 3)
+                second = util.wrap(second, opts_len + 3, ui.termwidth())
                 ui.write(" %-*s  %s\n" % (opts_len, first, second))
             else:
                 ui.write("%s\n" % first)
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -39,18 +39,18 @@ def moduledoc(file):
 
     return ''.join(result)
 
-def listexts(header, exts, maxlength):
+def listexts(ui, header, exts, maxlength):
     '''return a text listing of the given extensions'''
     if not exts:
         return ''
     # TODO: literal block is wrong, should be a field list or a simple table.
     result = '\n%s\n\n ::\n\n' % header
     for name, desc in sorted(exts.iteritems()):
-        desc = util.wrap(desc, maxlength + 5)
+        desc = util.wrap(desc, maxlength + 5, ui.termwidth())
         result += '  %s   %s\n' % (name.ljust(maxlength), desc)
     return result
 
-def extshelp():
+def extshelp(ui):
     doc = _(r'''
     Mercurial has the ability to add new features through the use of
     extensions. Extensions may add new commands, add options to
@@ -88,10 +88,10 @@ def extshelp():
     ''')
 
     exts, maxlength = extensions.enabled()
-    doc += listexts(_('enabled extensions:'), exts, maxlength)
+    doc += listexts(ui, _('enabled extensions:'), exts, maxlength)
 
     exts, maxlength = extensions.disabled()
-    doc += listexts(_('disabled extensions:'), exts, maxlength)
+    doc += listexts(ui, _('disabled extensions:'), exts, maxlength)
 
     return doc
 
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -379,3 +379,26 @@ class ui(object):
                      % (topic, item, pos, total, units, pct))
         else:
             ui.debug('%s:%s %s%s\n' % (topic, item, pos, units))
+
+    def termwidth(self):
+        try:
+            return int(self.config('ui', 'width', os.environ['COLUMNS']))
+        except (KeyError, ValueError):
+            pass
+        try:
+            import termios, array, fcntl
+            for dev in (sys.stdout, sys.stdin):
+                try:
+                    try:
+                        fd = dev.fileno()
+                    except AttributeError:
+                        continue
+                    if not os.isatty(fd):
+                        continue
+                    arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
+                    return array.array('h', arri)[1]
+                except ValueError:
+                    pass
+        except ImportError:
+            pass
+        return 80
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1248,33 +1248,7 @@ def uirepr(s):
     # Avoid double backslash in Windows path repr()
     return repr(s).replace('\\\\', '\\')
 
-def termwidth():
-    if 'COLUMNS' in os.environ:
-        try:
-            return int(os.environ['COLUMNS'])
-        except ValueError:
-            pass
-    try:
-        import termios, array, fcntl
-        for dev in (sys.stdout, sys.stdin):
-            try:
-                try:
-                    fd = dev.fileno()
-                except AttributeError:
-                    continue
-                if not os.isatty(fd):
-                    continue
-                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
-                return array.array('h', arri)[1]
-            except ValueError:
-                pass
-    except ImportError:
-        pass
-    return 80
-
-def wrap(line, hangindent, width=None):
-    if width is None:
-        width = termwidth() - 2
+def wrap(line, hangindent, width):
     padding = '\n' + ' ' * hangindent
     return padding.join(textwrap.wrap(line, width=width - hangindent))
 


More information about the Mercurial-devel mailing list