shithub: choc

Download patch

ref: 9690b06aaa27fdeb539b9d279d977bdaf98b3f48
parent: ceefa5bbd95c97d5299f8a81f6ce83a1ae83c55b
author: Simon Howard <fraggle@soulsphere.org>
date: Fri Mar 16 17:48:55 EDT 2018

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.

--- a/man/docgen
+++ b/man/docgen
@@ -35,6 +35,7 @@
 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 @@
 
         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
+        start += " " * (indent - len(start))
 
-        line += " " * (w - len(line))
-
         # 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):
+        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)
 
-            # Break onto the next line?
+        linesep = "\n" + " " * indent
 
-            if len(line) + len(word) + 1 > 75:
-                result += line + "\n"
-                line = " " * w
-
-            # Add another word
-
-            line += word + " "
-
-        result += line + "\n\n"
-
-        return result
+        return (start +
+                linesep.join(" ".join(line) for line in outlines) +
+                "\n\n")
 
     def completion_output(self, w):