Skip to content

Commit 083189d

Browse files
author
Robert James Oxspring
committed
Commented the more complex methods
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@161581 13f79535-47bb-0310-9956-ffa450edef68
1 parent 648c03a commit 083189d

1 file changed

Lines changed: 65 additions & 9 deletions

File tree

src/java/org/apache/commons/cli2/util/HelpFormatter.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,35 @@ public HelpFormatter(
143143
final String gutterCenter,
144144
final String gutterRight,
145145
final int fullWidth) {
146+
147+
// default the left gutter to empty string
146148
if (gutterLeft == null) {
147149
this.gutterLeft = "";
148150
}
149151
else {
150152
this.gutterLeft = gutterLeft;
151153
}
152154

155+
// default the center gutter to empty string
153156
if (gutterCenter == null) {
154157
this.gutterCenter = "";
155158
}
156159
else {
157160
this.gutterCenter = gutterCenter;
158161
}
159162

163+
// default the right gutter to empty string
160164
if (gutterRight == null) {
161165
this.gutterRight = "";
162166
}
163167
else {
164168
this.gutterRight = gutterRight;
165169
}
166170

171+
// calculate the available page width
167172
this.pageWidth = fullWidth - gutterLeft.length() - gutterRight.length();
173+
174+
// check available page width is valid
168175
if (fullWidth - pageWidth + gutterCenter.length() < 2) {
169176
throw new IllegalArgumentException(
170177
"The gutter strings leave no space for output! "
@@ -210,39 +217,54 @@ public void printHelp() throws IOException {
210217
else {
211218
option = group;
212219
}
213-
220+
221+
// grab the HelpLines to display
214222
final List helpLines = option.helpLines(0, displaySettings, comparator);
223+
224+
// calculate the maximum width of the usage strings
215225
int usageWidth = 0;
216226
for (final Iterator i = helpLines.iterator(); i.hasNext();) {
217227
final HelpLine helpLine = (HelpLine)i.next();
218228
final String usage = helpLine.usage(lineUsageSettings, comparator);
219229
usageWidth = Math.max(usageWidth, usage.length());
220230
}
231+
232+
// build a blank string to pad wrapped descriptions
221233
final StringBuffer blankBuffer = new StringBuffer();
222234
for (int i = 0; i < usageWidth; i++) {
223235
blankBuffer.append(' ');
224236
}
237+
238+
// determine the width available for descriptions
225239
final int descriptionWidth = Math.max(1,
226240
pageWidth - gutterCenter.length() - usageWidth);
241+
242+
// display each HelpLine
227243
for (final Iterator i = helpLines.iterator(); i.hasNext();) {
244+
245+
// grab the HelpLine
228246
final HelpLine helpLine = (HelpLine)i.next();
229-
final List descriptionLines =
247+
248+
// wrap the description
249+
final List descList =
230250
wrap(helpLine.getDescription(), descriptionWidth);
231-
final Iterator j = descriptionLines.iterator();
251+
final Iterator descriptionIterator = descList.iterator();
232252

253+
// display usage + first line of description
233254
printGutterLeft();
234255
pad(helpLine.usage(lineUsageSettings, comparator), usageWidth, out);
235256
out.print(gutterCenter);
236-
pad((String)j.next(), descriptionWidth, out);
257+
pad((String)descriptionIterator.next(), descriptionWidth, out);
237258
printGutterRight();
238259
out.println();
239260

240-
while (j.hasNext()) {
261+
// display padding + remaining lines of description
262+
while (descriptionIterator.hasNext()) {
241263
printGutterLeft();
242264
//pad(helpLine.getUsage(),usageWidth,out);
243265
out.print(blankBuffer);
244266
out.print(gutterCenter);
245-
pad((String)j.next(), descriptionWidth, out);
267+
pad((String)descriptionIterator.next(), descriptionWidth, out);
246268
printGutterRight();
247269
out.println();
248270
}
@@ -333,7 +355,9 @@ protected static void pad(
333355
final int width,
334356
final Writer writer)
335357
throws IOException {
336-
int left;
358+
final int left;
359+
360+
// write the text and record how many characters written
337361
if (text == null) {
338362
left = 0;
339363
}
@@ -342,16 +366,20 @@ protected static void pad(
342366
left = text.length();
343367
}
344368

369+
// pad remainder with spaces
345370
for (int i = left; i < width; ++i) {
346371
writer.write(' ');
347372
}
348373
}
349374

350375
protected static List wrap(final String text, final int width) {
376+
377+
// check for valid width
351378
if(width<1){
352379
throw new IllegalArgumentException("width must be positive");
353380
}
354381

382+
// handle degenerate case
355383
if (text == null) {
356384
return Collections.singletonList("");
357385
}
@@ -360,45 +388,73 @@ protected static List wrap(final String text, final int width) {
360388
final char[] chars = text.toCharArray();
361389
int left = 0;
362390

391+
// for each character in the string
363392
while (left < chars.length) {
393+
// sync left and right indeces
364394
int right = left;
395+
396+
// move right until we run out of characters, width or find a newline
365397
while (right < chars.length && chars[right] != '\n' && right<left+width+1) {
366398
right++;
367399
}
400+
401+
// if a newline was found
368402
if (right<chars.length && chars[right] == '\n') {
403+
// record the substring
369404
final String line = new String(chars, left, right - left);
370405
lines.add(line);
406+
// move to the end of the substring
371407
left = right + 1;
372408
if (left == chars.length) {
373409
lines.add("");
374410
}
411+
// restart the loop
375412
continue;
376413
}
377-
414+
415+
// move to the next ideal wrap point
378416
right = left + width - 1;
417+
418+
// if we have run out of characters
379419
if (chars.length <= right) {
420+
// record the substring
380421
final String line =
381422
new String(chars, left, chars.length - left);
382423
lines.add(line);
424+
425+
// abort the loop
383426
break;
384427
}
428+
429+
// back track the substring end until a space is found
385430
while (right >= left && chars[right] != ' ') {
386431
right--;
387432
}
433+
434+
// if a space was found
388435
if (right >= left) {
436+
// record the substring to space
389437
final String line = new String(chars, left, right - left);
390438
lines.add(line);
391-
left = right;
439+
440+
// absorb all the spaces before next substring
392441
while (right < chars.length && chars[right] == ' ') {
393442
right++;
394443
}
395444
left = right;
445+
446+
// restart the loop
396447
continue;
397448
}
398449

450+
// move to the wrap position irrespective of spaces
399451
right = Math.min(left + width, chars.length);
452+
453+
// record the substring
400454
final String line = new String(chars, left, right - left);
401455
lines.add(line);
456+
457+
// absorb any the spaces before next substring
402458
while (right < chars.length && chars[right] == ' ') {
403459
right++;
404460
}

0 commit comments

Comments
 (0)