commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r961374 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/HelpFormatter.java test/java/org/apache/commons/cli/HelpFormatterTest.java
Date Wed, 07 Jul 2010 13:48:13 GMT
Author: ebourg
Date: Wed Jul  7 13:48:12 2010
New Revision: 961374

URL: http://svn.apache.org/viewvc?rev=961374&view=rev
Log:
Ensure the wrap width is never exceeded and cut the words longer that the width if necessary
(CLI-193)

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java?rev=961374&r1=961373&r2=961374&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java Wed Jul
 7 13:48:12 2010
@@ -889,7 +889,8 @@ public class HelpFormatter
      * Finds the next text wrap position after <code>startPos</code> for the
      * text in <code>text</code> with the column width <code>width</code>.
      * The wrap point is the last position before startPos+width having a 
-     * whitespace character (space, \n, \r).
+     * whitespace character (space, \n, \r). If there is no whitespace character
+     * before startPos+width, it will return startPos+width.
      *
      * @param text The text being searched for the wrap position
      * @param width width of the wrapped text
@@ -900,8 +901,8 @@ public class HelpFormatter
      */
     protected int findWrapPos(String text, int width, int startPos)
     {
-        int pos = -1;
-
+        int pos;
+        
         // the line ends before the max wrap pos or a new line char found
         if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
                 || ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
@@ -931,17 +932,10 @@ public class HelpFormatter
             return pos;
         }
         
-        // must look for the first whitespace chearacter after startPos 
-        // + width
+        // if we didn't find one, simply chop at startPos+width
         pos = startPos + width;
-
-        while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
-               && (c != '\n') && (c != '\r'))
-        {
-            ++pos;
-        }
-
-        return (pos == text.length()) ? (-1) : pos;
+        
+        return pos == text.length() ? -1 : pos;
     }
 
     /**

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java?rev=961374&r1=961373&r2=961374&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java Wed
Jul  7 13:48:12 2010
@@ -46,9 +46,27 @@ public class HelpFormatterTest extends T
         // starting from 8 must give -1 - the wrap pos is after end
         assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
         
-        // if there is no a good position before width to make a wrapping look for the next
one
+        // words longer than the width are cut
         text = "aaaa aa";
-        assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
+        assertEquals("wrap position 3", 3, hf.findWrapPos(text, 3, 0));
+        
+        // last word length is equal to the width
+        text = "aaaaaa aaaaaa";
+        assertEquals("wrap position 4", 6, hf.findWrapPos(text, 6, 0));
+        assertEquals("wrap position 4", -1, hf.findWrapPos(text, 6, 7));
+    }
+
+    public void testRenderWrappedTextWordCut()
+    {
+        int width = 7;
+        int padding = 0;
+        String text = "Thisisatest.";
+        String expected = "Thisisa" + EOL + 
+                          "test.";
+        
+        StringBuffer sb = new StringBuffer();
+        new HelpFormatter().renderWrappedText(sb, width, padding, text);
+        assertEquals("cut and wrap", expected, sb.toString());
     }
 
     public void testRenderWrappedTextSingleLine()



Mime
View raw message