Changeset 583

Show
Ignore:
Timestamp:
07/26/08 01:39:32 (4 months ago)
Author:
athomas
Message:
  • Do not force termwidth() to 80 if stdout is not a terminal. Instead, return
    -1 and let the caller decide what to do.
  • Modified print_table() to not wrap cells in this case.
  • Docstring updates. More to come.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/builder.py

    r582 r583  
    514514        Upcalling is necessary. 
    515515 
    516         :namespace: The XML namespace of the attribute. Compare against 
     516        :param namespace: The XML namespace of the attribute. Compare against 
    517517                    XMLGrammar.EVAL_NS to determine if forced evaluation 
    518518                    can/should occur. 
    519         :name: Attribute name. 
    520         :value: Value to cast. 
    521  
    522         :returns: Tuple of (value, options) where options is a dictionary of 
    523                   extra Node constructor arguments. 
     519        :param name: Attribute name. 
     520        :param value: Value to cast. 
     521 
     522        :rtype: Tuple of (value, options) where options is a dictionary of 
     523                extra Node constructor arguments. 
    524524        """ 
    525525        casts = { 
  • cly/trunk/cly/console.py

    r570 r583  
    354354 
    355355def termwidth(): 
    356     """Guess the current terminal width.""" 
     356    """Guess the current terminal width. 
     357 
     358    Returns -1 if the terminal width can not be determined. 
     359    """ 
    357360    try: 
    358361        import curses 
    359362        return curses.tigetnum('cols') 
    360363    except: 
    361         return int(os.environ.get('COLUMNS', 80)) 
     364        return int(os.environ.get('COLUMNS', -1)) 
    362365 
    363366 
    364367def termheight(): 
    365     """Guess the current terminal height.""" 
     368    """Guess the current terminal height. 
     369 
     370    Returns -1 if the terminal height can not be determined. 
     371    """ 
    366372    try: 
    367373        import curses 
    368374        return curses.tigetnum('lines') 
    369375    except: 
    370         return int(os.environ.get('LINES', 25)) 
     376        return int(os.environ.get('LINES', -1)) 
    371377 
    372378 
     
    405411    if width is None: 
    406412        width = termwidth() 
     413        if width == -1: 
     414            return [rtext] 
    407415    out = [] 
    408416    for text in rtext.splitlines(): 
     
    444452    if width is None: 
    445453        width = termwidth() 
     454        if width == -1: 
     455            return text 
    446456    text = cwraptext(text, width) 
    447457    out = '' 
     
    455465    if width is None: 
    456466        width = termwidth() 
     467        if width == -1: 
     468            return text 
    457469    text = cwraptext(text, width) 
    458470    out = '' 
     
    464476def print_table(header, table, sep=u' ', indent=u'', expand_to_fit=True, 
    465477                header_format='^B^U', row_format=('^6', '^B^6'), 
    466                 min_widths=None): 
     478                min_widths=None, term_width=None): 
    467479    """Print a list of lists as a table, so that columns line up nicely. 
    468480 
    469     ``header``: list of column headings 
    470         Will be printed as the first row. 
    471  
    472     ``table``: list of rows 
    473         Data to print. 
    474  
    475     ``sep=' '``: string 
    476         The column separator. 
    477  
    478     ``indent=''``: string 
    479         Table indentation as a string. 
    480  
    481     ``header_format='^B^U'``: string 
    482         Formatting to use for header. 
    483  
    484     ``row_format=('^6', '^B^6')``: tuple 
    485         A tuple specifying cycling formatting colours to use for each row. 
    486  
    487     ``expand_to_fit=True``: boolean or integer 
    488         If a boolean, signifies whether print_table should expand the table to 
    489         the width of the terminal or compact it as much as possible. If an 
    490         integer, specifies the width to expand to. 
    491  
    492     ``min_widths``: list of minimum column widths 
    493         Columns will be guaranteed to be at least the width of each element 
    494         in the list. 
    495  
    496     Note: ``print_table`` supports the ``^R`` formatting code, in addition to 
    497     those supported by cprint, which corresponds to the colour formatting of 
    498     the current table row. 
     481    Arguments: 
     482        :header: List of column headings. Will be printed as the first row. 
     483 
     484        :table: List of lists for the table body. 
     485        :sep: The column separator. 
     486        :indent: Table indentation as a string. 
     487        :header_format: Formatting to use for header. 
     488        :row_format: A tuple specifying cycling formatting colours to use for 
     489                     each row. 
     490        :expand_to_fit: If a boolean, signifies whether print_table should 
     491                        expand the table to the width of the terminal or 
     492                        compact it as much as possible. If an integer, 
     493                        specifies the width to expand to. 
     494        :min_widths: Columns will be guaranteed to be at least the width of 
     495                     each element in this list. May also be a dictionary of 
     496                     column indices to widths. 
     497        :term_width: Specify the terminal width. 
     498 
     499    :returns: 
     500        Stuff. 
     501 
     502    Note: In addition to the normal formatting codes supported by :func:`cprint`, 
     503    :func:`print_table` supports the ``^R`` formatting code, which corresponds 
     504    to the colour formatting of the current table row. 
    499505    """ 
    500506    def ctlen(s): 
     
    507513 
    508514    # Scale size_hints percentages to terminal width 
    509     term_width = termwidth() - (columns - 1) * seplen - ctlen(indent) 
     515    if term_width is None: 
     516        term_width = termwidth() 
     517        if term_width == -1: 
     518            term_width = max([sum(map(ctlen, r)) + len(r) * 2 for r in rows]) 
     519        else: 
     520            term_width = term_width - (columns - 1) * seplen - ctlen(indent) 
    510521    if not isinstance(min_widths, dict): 
    511522        min_widths = dict(enumerate(min_widths or []))