Re: Long help lines and wrapping

Bob van der Poel <[email protected]> Wed, 13 Nov 2002 19:00:46 -0700
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
Greg Ward wrote:
> 
> On 10 November 2002, Bob van der Poel said:
> > Opps, no snidness intended. I quite like optik and think it is a
> > valuable addition to the python suite. Sorry if my feeble attempt
> > at lightness/humour was misinterpreted.
> 
> Well, *I* wasn't offended, but I'm also prone to software
> anthropomorphy.
> 
> > Replace the wrap_text() function called in format_option() with a
> > routine specific to optik. This would follow the rules in the existing
> > wrap_text() with the following addition: if a \n is found in the text to
> > be wrapped, the text will be wrapped at this point. In addition, any
> > white space following the \n will be preserved.
> >
> > If this was to work I could then set up my option like:
> >
> >         parser.add_option("-p", "--pattern", type="string",
> >            action="store", dest="pattern",
> >            help="Set Pattern. Available patterns are:\n" +
> >                 "   xx use the xx pattern\n" +
> >                 "   yy use the yy pattern")
> 
> This sounds eminently sensible to me.  Easy to implement too.  I think
> David's idea is massive flaming overkill.
> 
> > Just thinking a bit about this myself, I see a pro and a con.
> >
> > Con: If existing code with '\n's already in the help strings used the
> > new routine, it might break. However, I don't see this being a big
> > issue? I may be wrong...
> 
> That doesn't bother me.
> 
> > Pro: Setting up help strings in this manner, esp. with """ strings,
> > seems to be quite intutitive.
> 
> Yep.
> 
> > Question: If I were to impliment this by adding a new wrap_text() is
> > there a way this could be done without hacking into the optik code?
> 
> No.  Furthermore, I need to change Optik to use the new textwrap module
> that will be in the Python 2.3 standard library.  It's not *that*
> different from the wrap_text() function from distutils (I wrote 'em
> both), but any change you make *might* be made obsolete when I do that.
> 
> IOW: patches welcome, but any patch you supply is more likely to kick me
> into working on Optik again than to be checked in.  That might not be a
> bad thing.  ;-)


Okay. Here's a reworked function for help.py. All I've done is to split
the option string into chucks at the newlines. The rest for the code is
unchanged (umm, did have to change a variable name in the "help_lines="
line. I did some simple tests and it seems to work just fine :)

Advantage is that I didn't need to hack on the wrapping routines. BTW,
it also effects the 'usage' line, so at least that is consistent.

Don't know what you want to do with this? If I just fix up my copy
that's fine for me, but if I decide to distribute the code... then the
others will get funny formatted lines.

 def format_option (self, option):
        # The help for each option consists of two parts:
        #   * the opt strings and metavars
        #     eg. ("-x", or "-fFILENAME, --file=FILENAME")
        #   * the user-supplied help string
        #     eg. ("turn on expert mode", "read data from FILENAME")
        #
        # If possible, we write both of these on the same line:
        #   -x      turn on expert mode
        #
        # But if the opt string list is too long, we put the help
        # string on a second line, indented to the same column it would
        # start in if it fit on the first line.
        #   -fFILENAME, --file=FILENAME
        #           read data from FILENAME
        result = []
        opts = option.option_strings
        opt_width = self.help_position - self.current_indent - 2
        if len(opts) > opt_width:
            opts = "%*s%s\n" % (self.current_indent, "", opts)
            indent_first = self.help_position
        else:                       # start help on same line as opts
            opts = "%*s%-*s  " % (self.current_indent, "", opt_width,
opts)
            indent_first = 0
        result.append(opts)
        if option.help:
            help_list = option.help.split('\n')                     # bv
            for help_lines in help_list:                            # bv
               help_lines = wrap_text(help_lines, self.help_width)  # bv
               result.append("%*s%s\n" % (indent_first, "",
help_lines[0]))
               result.extend(["%*s%s\n" % (self.help_position, "", line)
                           for line in help_lines[1:]])
        elif opts[-1] != "\n":
            result.append("\n")
        return "".join(result)




-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: [email protected]
WWW:   http://www.kootenay.com/~bvdpoel



-------------------------------------------------------
This sf.net email is sponsored by: Are you worried about 
your web server security? Click here for a FREE Thawte 
Apache SSL Guide and answer your Apache SSL security 
needs: http://www.gothawte.com/rd523.html