foxygit / doom Log in
commit 9690b06aaa27fdeb539b9d279d977bdaf98b3f48
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Fri Mar 16 21:48:55 2018 -0400
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Fri Mar 16 21:48:55 2018 -0400

    docgen: Fix spurious whitespace.

    The text output mode included useless whitespace at the end of lines
    and also occasionally generated lines containing nothing but
    whitespace. Clean up the code that generates text output and fix
    these bugs.
---
 man/docgen | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/man/docgen b/man/docgen
index cdaac238..3f521e3a 100755
--- a/man/docgen
+++ b/man/docgen
@@ -35,6 +35,7 @@ import re
 import glob
 import getopt

+TEXT_WRAP_WIDTH = 78
 INCLUDE_STATEMENT_RE = re.compile("@include\s+(\S+)")

 # Use appropriate stdout function for Python 2 or 3
@@ -261,46 +262,38 @@ class Parameter:

         return result

-    def plaintext_output(self, w):
-
+    def plaintext_output(self, indent):
         # Build the first line, with the argument on
-
-        line = "  " + self.name
+        start = "  " + self.name
         if self.args:
-            line += " " + self.args
+            start += " " + self.args

         # pad up to the plaintext width
-
-        line += " " * (w - len(line))
+        start += " " * (indent - len(start))

         # Build the description text
-
         description = self.text
-
         if self.platform:
             description += " (%s only)" % self.platform
-
         description += self._games_only_text()

         # Build the complete text for the argument
         # Split the description into words and add a word at a time
-
         result = ""
-        for word in re.split('\s+', description):
-
-            # Break onto the next line?
-
-            if len(line) + len(word) + 1 > 75:
-                result += line + "\n"
-                line = " " * w
-
-            # Add another word
-
-            line += word + " "
-
-        result += line + "\n\n"
-
-        return result
+        words = [word for word in re.split('\s+', description) if word]
+        maxlen = TEXT_WRAP_WIDTH - indent
+        outlines = [[]]
+        for word in words:
+            linelen = sum(len(w) + 1 for w in outlines[-1])
+            if linelen + len(word) > maxlen:
+                outlines.append([])
+            outlines[-1].append(word)
+
+        linesep = "\n" + " " * indent
+
+        return (start +
+                linesep.join(" ".join(line) for line in outlines) +
+                "\n\n")

     def completion_output(self, w):