Issue Details (XML | Word | Printable)

Key: CLI-151
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Dan Armbrust
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Commons CLI

HelpFormatter wraps incorrectly on every line beyond the first

Created: 18/Feb/08 10:20 PM   Updated: 25/May/09 05:26 PM
Return to search
Component/s: Help formatter
Affects Version/s: None
Fix Version/s: 1.2

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works fix-wrapping.patch 2008-04-17 01:38 PM J. Lewis Muir 1 kB
Issue Links:
Duplicate
 

Resolution Date: 18/Feb/09 05:20 AM


 Description  « Hide
The method findWrapPos(...) in the HelpFormatter is a couple of bugs in the way that it deals with the "startPos" variable. This causes it to format every line beyond the first line by "startPos" to many characters, beyond the specified width.

To see this, create an option with a long description, and then use the help formatter to print it. The first line will be the correct length. The 2nd, 3rd, etc lines will all be too long.

I don't have a patch (sorry) - but here is a corrected version of the method.

I fixed it in two places - both were using "width + startPos" when they should have been using width.

protected int findWrapPos(String text, int width, int startPos)
    {
        int pos = -1;

        // 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))
        {
            return pos+1;
        }
        else if ((width) >= text.length())
        {
            return -1;
        }


        // look for the last whitespace character before startPos+width
        pos = width;

        char c;

        while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ')
               && (c != '\n') && (c != '\r'))
        {
            --pos;
        }

        // if we found it - just return
        if (pos > startPos)
        {
            return pos;
        }
        
        // must look for the first whitespace chearacter after startPos 
        // + width
        pos = startPos + width;

        while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
               && (c != '\n') && (c != '\r'))
        {
            ++pos;
        }

        return (pos == text.length())        ? (-1) : pos;
    }


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
No work has yet been logged on this issue.