Make sure prompts with filenames don't break mid-filename. trunk

Thu, 02 Apr 2009 22:31:07 -0400

author
Brett Smith <brettcsmith@brettcsmith.org>
date
Thu, 02 Apr 2009 22:31:07 -0400
branch
trunk
changeset 105
f76ac41fe061
parent 104
ee9600dac90a
child 106
dcf005ef7070

Make sure prompts with filenames don't break mid-filename.

NEWS file | annotate | diff | comparison | revisions
scripts/dtrx file | annotate | diff | comparison | revisions
--- a/NEWS	Wed Mar 25 22:58:57 2009 -0400
+++ b/NEWS	Thu Apr 02 22:31:07 2009 -0400
@@ -12,8 +12,8 @@
    instead of "rename the file."  This wording has been fixed, along with
    some other wording adjustments in the prompts generally.
 
- * Perform more reliable detection of the terminal size, and wrap more
-   prompts accordingly.
+ * Perform more reliable detection of the terminal size, and improve word
+   wrapping on prompts.
 
 Version 6.4
 -----------
--- a/scripts/dtrx	Wed Mar 25 22:58:57 2009 -0400
+++ b/scripts/dtrx	Thu Apr 02 22:31:07 2009 -0400
@@ -669,8 +669,8 @@
         width = struct.unpack("HHHH", size)[1]
     except IOError:
         width = 80
-    q_wrapper = textwrap.TextWrapper(width=width - 1, break_long_words=False)
-    choice_wrapper = textwrap.TextWrapper(width=width - 1, initial_indent=' * ',
+    width = width - 1
+    choice_wrapper = textwrap.TextWrapper(width=width, initial_indent=' * ',
                                           subsequent_indent='   ',
                                           break_long_words=False)
     
@@ -696,6 +696,19 @@
             except KeyError:
                 print
 
+    def wrap(self, question, *args):
+        words = question.split()
+        for arg in args:
+            words[words.index('%s')] = arg
+        result = [words.pop(0)]
+        for word in words:
+            extend = '%s %s' % (result[-1], word)
+            if len(extend) > self.width:
+                result.append(word)
+            else:
+                result[-1] = extend
+        return result
+
     def __cmp__(self, other):
         return cmp(self.current_policy, other)
     
@@ -726,9 +739,9 @@
             raise ValueError("bad value %s for default policy" % (default,))
 
     def prep(self, archive_filename, extractor):
-        question = self.q_wrapper.wrap(
-            "%s contains one %s, but its name doesn't match." %
-            (archive_filename, extractor.content_type))
+        question = self.wrap(
+            "%s contains one %s but its name doesn't match.",
+            archive_filename, extractor.content_type)
         question.append(" Expected: " + extractor.basename())
         question.append("   Actual: " + extractor.content_name)
         choice_vars = (extractor.content_type, extractor.basename())
@@ -763,9 +776,9 @@
         if (self.permanent_policy is not None) or (archive_count == 0):
             self.current_policy = self.permanent_policy or RECURSE_NOT_NOW
             return
-        question = self.q_wrapper.wrap(
-            "%s contains %s other archive file(s), out of %s file(s) total." %
-            (current_filename, archive_count, extractor.file_count))
+        question = self.wrap(
+            "%s contains %s other archive file(s), out of %s file(s) total.",
+            current_filename, archive_count, extractor.file_count)
         if target == '.':
             target = ''
         included_root = extractor.included_root

mercurial