Skip to content

Commit 777cb81

Browse files
committed
Ensure the wrap width is never exceeded and cut the words longer that the width if necessary (CLI-193)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@961374 13f79535-47bb-0310-9956-ffa450edef68
1 parent 23df383 commit 777cb81

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/main/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
889889
* Finds the next text wrap position after <code>startPos</code> for the
890890
* text in <code>text</code> with the column width <code>width</code>.
891891
* The wrap point is the last position before startPos+width having a
892-
* whitespace character (space, \n, \r).
892+
* whitespace character (space, \n, \r). If there is no whitespace character
893+
* before startPos+width, it will return startPos+width.
893894
*
894895
* @param text The text being searched for the wrap position
895896
* @param width width of the wrapped text
@@ -900,8 +901,8 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
900901
*/
901902
protected int findWrapPos(String text, int width, int startPos)
902903
{
903-
int pos = -1;
904-
904+
int pos;
905+
905906
// the line ends before the max wrap pos or a new line char found
906907
if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
907908
|| ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
@@ -931,17 +932,10 @@ else if (startPos + width >= text.length())
931932
return pos;
932933
}
933934

934-
// must look for the first whitespace chearacter after startPos
935-
// + width
935+
// if we didn't find one, simply chop at startPos+width
936936
pos = startPos + width;
937-
938-
while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
939-
&& (c != '\n') && (c != '\r'))
940-
{
941-
++pos;
942-
}
943-
944-
return (pos == text.length()) ? (-1) : pos;
937+
938+
return pos == text.length() ? -1 : pos;
945939
}
946940

947941
/**

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,27 @@ public void testFindWrapPos() throws Exception
4646
// starting from 8 must give -1 - the wrap pos is after end
4747
assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
4848

49-
// if there is no a good position before width to make a wrapping look for the next one
49+
// words longer than the width are cut
5050
text = "aaaa aa";
51-
assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
51+
assertEquals("wrap position 3", 3, hf.findWrapPos(text, 3, 0));
52+
53+
// last word length is equal to the width
54+
text = "aaaaaa aaaaaa";
55+
assertEquals("wrap position 4", 6, hf.findWrapPos(text, 6, 0));
56+
assertEquals("wrap position 4", -1, hf.findWrapPos(text, 6, 7));
57+
}
58+
59+
public void testRenderWrappedTextWordCut()
60+
{
61+
int width = 7;
62+
int padding = 0;
63+
String text = "Thisisatest.";
64+
String expected = "Thisisa" + EOL +
65+
"test.";
66+
67+
StringBuffer sb = new StringBuffer();
68+
new HelpFormatter().renderWrappedText(sb, width, padding, text);
69+
assertEquals("cut and wrap", expected, sb.toString());
5270
}
5371

5472
public void testRenderWrappedTextSingleLine()

0 commit comments

Comments
 (0)