Changeset 420

Show
Ignore:
Timestamp:
05/08/07 21:17:03 (2 years ago)
Author:
athomas
Message:

cly: Added some more cly.console.print_table configurability: the ability to
expand to a specified width and the ability to specify an initial indent.

Files:

Legend:

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

    r417 r420  
    340340 
    341341 
    342 def print_table(header, table, sep=' ', auto_format=('^B^U', '^6', '^B^6'), expand_to_fit=1): 
     342def print_table(header, table, sep=' ', indent='', 
     343                auto_format=('^B^U', '^6', '^B^6'), expand_to_fit=True): 
    343344    """Print a list of lists as a table, so that columns line up nicely. 
    344345 
     
    351352        Data to print. 
    352353 
    353     ``sep=' '`` 
     354    ``sep=' '``: string 
    354355        The column separator. 
     356 
     357    ``indent=''``: string 
     358        Table indentation as a string. 
    355359 
    356360    ``auto_format=('^B^U', '^6', '^B^2')``: tuple 
     
    359363        alternating rows. 
    360364 
    361     ``expand_to_fit=True``: boolean 
    362         Signifies whether print_table should expand the table to the width of 
    363         the terminal. 
     365    ``expand_to_fit=True``: boolean or integer 
     366        If a boolean, signifies whether print_table should expand the table to 
     367        the width of the terminal or compact it as much as possible. If an 
     368        integer, specifies the width to expand to. 
    364369 
    365370    Note: ``print_table`` supports the ``^R`` formatting code, in addition to 
     
    379384        if i < cols - 1: 
    380385            colwidths[i] += seplen 
    381     # Scale columns if total width is less than the terminal width, or user requested 
    382     if termwidth() < sum(colwidths) or expand_to_fit: 
    383         scale = float(termwidth() - 1) / float(sum(colwidths)) 
     386 
     387    # Scale columns if total width is less than the terminal width, or user 
     388    # requested 
     389    if expand_to_fit in (True, False): 
     390        max_width = termwidth() - len(indent) 
     391    else: 
     392        max_width = expand_to_fit 
     393        expand_to_fit = True 
     394 
     395    if expand_to_fit is True or max_width < sum(colwidths): 
     396        scale = float(max_width - 1) / float(sum(colwidths)) 
    384397        colwidths = [int(float(x) * scale) for x in colwidths] 
    385398        mincol = min(colwidths) 
    386399        for i, col in enumerate(colwidths): 
    387400            if col == mincol: 
    388                 colwidths[0] += termwidth() - sum(colwidths) 
     401                colwidths[0] += max_width - sum(colwidths) 
    389402 
    390403    auto_format = list(auto_format) 
     
    414427        # Emit 
    415428        for i in range(0, realrows): 
    416             cwrite(sys.stdout, fmt) 
     429            cwrite(sys.stdout, indent + fmt) 
    417430            for j, col in enumerate(xrow): 
    418431                slen = j < cols - 1 and seplen or 0