Skip to content

Commit beef673

Browse files
authored
Fixed CLI-351 (#400)
TextHelpAppendable.makeColumnQueue had multiple issues arising from an off-by-one issue.
1 parent 5d14bd0 commit beef673

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/main/java/org/apache/commons/cli/help/TextHelpAppendable.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static int indexOfWrap(final CharSequence text, final int width, final in
7878
}
7979
// handle case of width > text.
8080
// the line ends before the max wrap pos or a new line char found
81-
final int limit = Math.min(startPos + width, text.length() - 1);
81+
int limit = Math.min(startPos + width, text.length());
8282
for (int idx = startPos; idx < limit; idx++) {
8383
if (BREAK_CHAR_SET.contains(text.charAt(idx))) {
8484
return idx;
@@ -87,6 +87,8 @@ public static int indexOfWrap(final CharSequence text, final int width, final in
8787
if (startPos + width >= text.length()) {
8888
return text.length();
8989
}
90+
91+
limit = Math.min(startPos + width, text.length() - 1);
9092
int pos;
9193
// look for the last whitespace character before limit
9294
for (pos = limit; pos >= startPos; --pos) {
@@ -311,15 +313,15 @@ protected Queue<String> makeColumnQueue(final CharSequence columnData, final Tex
311313
final String indent = Util.repeatSpace(style.getIndent());
312314
final Queue<String> result = new LinkedList<>();
313315
int wrapPos = 0;
314-
int nextPos;
316+
int lastPos;
315317
final int wrappedMaxWidth = style.getMaxWidth() - indent.length();
316318
while (wrapPos < columnData.length()) {
317319
final int workingWidth = wrapPos == 0 ? style.getMaxWidth() : wrappedMaxWidth;
318-
nextPos = indexOfWrap(columnData, workingWidth, wrapPos);
319-
final CharSequence working = columnData.subSequence(wrapPos, nextPos);
320+
lastPos = indexOfWrap(columnData, workingWidth, wrapPos);
321+
final CharSequence working = columnData.subSequence(wrapPos, lastPos);
320322
result.add(lpad + style.pad(wrapPos > 0, working));
321-
wrapPos = Util.indexOfNonWhitespace(columnData, nextPos);
322-
wrapPos = wrapPos == -1 ? nextPos : wrapPos;
323+
wrapPos = Util.indexOfNonWhitespace(columnData, lastPos);
324+
wrapPos = wrapPos == -1 ? lastPos + 1 : wrapPos;
323325
}
324326
return result;
325327
}

src/test/java/org/apache/commons/cli/help/TextHelpAppendableTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void setUp() {
4949
}
5050

5151
@Test
52-
void tesstMakeColumnQueue() {
52+
void testMakeColumnQueue() {
5353
final String text = "The quick brown fox jumps over the lazy dog";
5454
final TextStyle.Builder styleBuilder = TextStyle.builder().setMaxWidth(10).setIndent(0).setLeftPad(0);
5555

@@ -98,6 +98,20 @@ void tesstMakeColumnQueue() {
9898
assertEquals(expected, result, "right aligned failed");
9999
}
100100

101+
@Test
102+
void testMakeColumnQueueWithMultipleTrailingLineBreaks() {
103+
// this test should work with any n>1 multiple trailing characters from TestHelpAppendable.BREAK_CHAR_SET.
104+
final String text = "Header\t" + (char) Character.PARAGRAPH_SEPARATOR;
105+
final TextStyle.Builder styleBuilder = TextStyle.builder().setMaxWidth(10).setIndent(0).setLeftPad(0);
106+
107+
Queue<String> expected = new LinkedList<>();
108+
expected.add("Header ");
109+
expected.add(" ");
110+
111+
Queue<String> result = underTest.makeColumnQueue(text, styleBuilder.get());
112+
assertEquals(expected, result, "left aligned failed");
113+
}
114+
101115
@Test
102116
void testAdjustTableFormat() {
103117
// test width smaller than header

0 commit comments

Comments
 (0)