[PATCH 3 of 3] help: format all output using RST

Olav Reinert seroton10 at gmail.com
Sat Jun 2 04:33:59 CDT 2012


# HG changeset patch
# User Olav Reinert <seroton10 at gmail.com>
# Date 1338629323 -7200
# Node ID 86c70b7cccce414ce9be04c9e65ab3e45ad2097e
# Parent  98a4e54433dbb02a26405b5e1652efe7276fceba
help: format all output using RST

This change is the last patch needed to implement help text generation based
only on formatting a single text object marked up with RST.

diff -r 98a4e54433db -r 86c70b7cccce mercurial/commands.py
--- a/mercurial/commands.py	Sat Jun 02 11:25:40 2012 +0200
+++ b/mercurial/commands.py	Sat Jun 02 11:28:43 2012 +0200
@@ -3097,16 +3097,18 @@
             # except block, nor can be used inside a lambda. python issue4617
             prefix = inst.args[0]
             select = lambda c: c.lstrip('^').startswith(prefix)
-            helplist(select)
-            return
+            rst = helplist(select)
+            return rst
+
+        rst = []
 
         # check if it's an invalid alias and display its error if it is
         if getattr(entry[0], 'badalias', False):
             if not unknowncmd:
+                ui.pushbuffer()
                 entry[0](ui)
-            return
-
-        rst = []
+                rst.append(ui.popbuffer())
+            return rst
 
         # synopsis
         if len(entry) > 2:
@@ -3165,10 +3167,7 @@
             elif not ui.quiet:
                 rst.append(_('\nuse "hg -v help %s" to show more info\n')
                            % name)
-
-        keep = ui.verbose and ['verbose'] or []
-        formatted, pruned = minirst.format(''.join(rst), textwidth, keep=keep)
-        ui.write(formatted)
+        return rst
 
 
     def helplist(select=None):
@@ -3201,11 +3200,12 @@
             h[f] = doc.splitlines()[0].rstrip()
             cmds[f] = c.lstrip("^")
 
+        rst = []
         if not h:
-            ui.status(_('no commands defined\n'))
-            return
-
-        rst = []
+            if not ui.quiet:
+                rst.append(_('no commands defined\n'))
+            return rst
+
         if not ui.quiet:
             rst.append(header)
         fns = sorted(h)
@@ -3254,7 +3254,7 @@
                 rst.append('\n%s\n' % title)
                 if options:
                     rst.append('\n%s\n' % help.optrst(options, ui.verbose))
-        ui.write(minirst.format(''.join(rst), textwidth))
+        return rst
 
     def helptopic(name):
         for names, header, doc in help.helptable:
@@ -3276,7 +3276,7 @@
                        'the %s command\n') % (name, name))
         except error.UnknownCommand:
             pass
-        ui.write(minirst.format(''.join(rst), textwidth))
+        return rst
 
     def helpext(name):
         try:
@@ -3292,10 +3292,10 @@
             head, tail = doc, ""
         else:
             head, tail = doc.split('\n', 1)
-        ui.write(_('%s extension - %s\n\n') % (name.split('.')[-1], head))
+        rst = [_('%s extension - %s\n\n') % (name.split('.')[-1], head)]
         if tail:
-            ui.write(minirst.format(tail, textwidth))
-            ui.status('\n')
+            rst.extend(tail.splitlines(True))
+            rst.append('\n')
 
         if mod:
             try:
@@ -3303,10 +3303,11 @@
             except AttributeError:
                 ct = {}
             modcmds = set([c.split('|', 1)[0] for c in ct])
-            helplist(modcmds.__contains__)
+            rst.extend(helplist(modcmds.__contains__))
         else:
-            ui.write(_('use "hg help extensions" for information on enabling '
+            rst.append(_('use "hg help extensions" for information on enabling '
                        'extensions\n'))
+        return rst
 
     def helpextcmd(name):
         cmd, ext, mod = extensions.disabledcmd(ui, name,
@@ -3318,8 +3319,10 @@
         rst.append('\n')
         rst.append(_('use "hg help extensions" for information on enabling '
                    'extensions\n'))
-        ui.write(minirst.format(''.join(rst), textwidth))
-
+        return rst
+
+
+    rst = []
     kw = opts.get('keyword')
     if kw:
         matches = help.topicmatch(kw)
@@ -3328,12 +3331,9 @@
                          ('extensions', _('Extensions')),
                          ('extensioncommands', _('Extension Commands'))):
             if matches[t]:
-                ui.write('%s:\n\n' % title)
-                rst = ''.join(minirst.maketable(matches[t], 1))
-                ui.write(minirst.format(rst))
-        return
-
-    if name and name != 'shortlist':
+                rst.append('%s:\n\n' % title)
+                rst.extend(minirst.maketable(matches[t], 1))
+    elif name and name != 'shortlist':
         i = None
         if unknowncmd:
             queries = (helpextcmd,)
@@ -3345,7 +3345,7 @@
             queries = (helptopic, helpcmd, helpext, helpextcmd)
         for f in queries:
             try:
-                f(name)
+                rst = f(name)
                 i = None
                 break
             except error.UnknownCommand, inst:
@@ -3354,9 +3354,13 @@
             raise i
     else:
         # program name
-        ui.status(_("Mercurial Distributed SCM\n"))
-        ui.status('\n')
-        helplist()
+        if not ui.quiet:
+            rst = [_("Mercurial Distributed SCM\n"), '\n']
+        rst.extend(helplist())
+
+    keep = ui.verbose and ['verbose'] or []
+    formatted, pruned = minirst.format(''.join(rst), textwidth, keep=keep)
+    ui.write(formatted)
 
 
 @command('identify|id',
diff -r 98a4e54433db -r 86c70b7cccce tests/test-qrecord.t
--- a/tests/test-qrecord.t	Sat Jun 02 11:25:40 2012 +0200
+++ b/tests/test-qrecord.t	Sat Jun 02 11:28:43 2012 +0200
@@ -6,7 +6,8 @@
 help record (no record)
 
   $ hg help record
-  record extension - commands to interactively select changes for commit/qrefresh
+  record extension - commands to interactively select changes for
+  commit/qrefresh
   
   use "hg help extensions" for information on enabling extensions
 


More information about the Mercurial-devel mailing list