[PATCH] Format the help text to properly fit to the terminal window

Martin Kolman mkolman at redhat.com
Tue May 20 11:38:23 UTC 2014


On Mon, 2014-05-19 at 13:27 -0400, Anne Mulhern wrote:
> 
> 
> 
> ----- Original Message -----
> > From: "Martin Kolman" <mkolman at redhat.com>
> > To: anaconda-patches at lists.fedorahosted.org
> > Sent: Monday, May 19, 2014 9:28:26 AM
> > Subject: [PATCH] Format the help text to properly fit to the terminal window
> > 
> > Provided it is possible to detect the terminal window size.
> > 
> > Signed-off-by: Martin Kolman <mkolman at redhat.com>
> > ---
> >  pyanaconda/anaconda_argparse.py | 45
> >  +++++++++++++++++++++++++++++++++++++++--
> >  1 file changed, 43 insertions(+), 2 deletions(-)
> > 
> > diff --git a/pyanaconda/anaconda_argparse.py
> > b/pyanaconda/anaconda_argparse.py
> > index 090261d..c5ca2b9 100644
> > --- a/pyanaconda/anaconda_argparse.py
> > +++ b/pyanaconda/anaconda_argparse.py
> > @@ -25,14 +25,51 @@ DESCRIPTION = "Anaconda is the installation program used
> > by Fedora," \
> >  
> >  import itertools
> >  import os
> > +import sys
> > +import fcntl
> > +import termios
> > +import struct
> >  
> > -from argparse import ArgumentParser, ArgumentError
> > +from argparse import ArgumentParser, ArgumentError, HelpFormatter
> >  
> >  from pyanaconda.flags import BootArgs
> >  
> >  import logging
> >  log = logging.getLogger("anaconda")
> >  
> > +# Help text formatting constants
> > +
> > +LEFT_PADDING = 8  # the help text will start after 8 spaces
> > +RIGHT_PADDING = 8  # there will be 8 spaces left on the right
> > +DEFAULT_HELP_WIDTH = 80
> > +
> > +def get_help_width():
> > +    """
> > +    Try to detect the terminal window width size and use it to
> > +    compute optimal help text width. If it can't be detected
> > +    a default values is returned.
> > +
> > +    :returns: optimal help text width in number of characters
> > +    :rtype: integer
> > +    """
> > +
> > +    help_width = DEFAULT_HELP_WIDTH
> > +    try:
> > +        data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
> 
> You don't need the call to fileno(), seems ioctl will do it for you.
Oh, good to know that, fixing locally, thanks! :)
> 
> > +        columns = int(struct.unpack('hh', data)[1])
> > +        # apply the right padding
> > +        columns = columns - RIGHT_PADDING
> > +        if columns > 0:
> > +            help_width = columns
> > +    # pylint: disable=broad-except
> > +    except Exception as e:
> > +        # detection failed, use the default
> > +        # NOTE: this could be caused by the COLUMNS string having a value
> > +        # that can't be converted to an integer
> > +        print("anaconda argparse: terminal size detection failed, using
> > default width")
> > +        print(e)
> 
> Maybe this error would be better logged than printed.
We skip as much of anaconda initialization as possible when just
printing the help texts, so we skip also the Anaconda logging
infrastructure. So printing it out is IMHO the only option here to
report the error.

> 
> > +    return help_width
> > +
> >  class AnacondaArgumentParser(ArgumentParser):
> >      """
> >      Subclass of ArgumentParser that also examines boot arguments.
> > @@ -47,11 +84,15 @@ class AnacondaArgumentParser(ArgumentParser):
> >              False: accept the argument with or without the prefix.
> >              True: ignore the argument without the prefix. (default)
> >          """
> > +        help_width = get_help_width()
> >          self._boot_arg = dict()
> >          self.deprecated_bootargs = []
> >          self.bootarg_prefix = kwargs.pop("bootarg_prefix", "")
> >          self.require_prefix = kwargs.pop("require_prefix", True)
> > -        ArgumentParser.__init__(self, description=DESCRIPTION, *args,
> > **kwargs)
> > +        ArgumentParser.__init__(self, description=DESCRIPTION,
> > +                                formatter_class=lambda prog: HelpFormatter(
> > +                                    prog, max_help_position=LEFT_PADDING,
> > width=help_width),
> 
> with=help_width should be width=get_help_width()
I'm using the variable instead of calling the function on purpose. I've
found out that otherwise it would call the function for every argument,
so it is better to pass the number directly.

> 
> > +                                *args, **kwargs)
> >  
> >      def add_argument(self, *args, **kwargs):
> >          """
> > --
> > 1.9.0
> > 
> > _______________________________________________
> > anaconda-patches mailing list
> > anaconda-patches at lists.fedorahosted.org
> > https://lists.fedorahosted.org/mailman/listinfo/anaconda-patches
> > 
> 
> - mulhern
> _______________________________________________
> anaconda-patches mailing list
> anaconda-patches at lists.fedorahosted.org
> https://lists.fedorahosted.org/mailman/listinfo/anaconda-patches




More information about the anaconda-patches mailing list