Changeset 275

Show
Ignore:
Timestamp:
10/06/05 02:49:27 (3 years ago)
Author:
athomas
Message:
  • print_table() now automatically colours tables. Can be disabled by passing 'auto_format = False'.
Files:

Legend:

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

    r274 r275  
    8787    return '\n'.join(textwrap.wrap(text, termwidth(), **argd)) 
    8888 
    89 def print_table(table, header=None, sep=' ', numfmt='%g', highlight_header = True, alternate_rows = ('^1', '^2')): 
     89def print_table(table, header=None, sep=' ', numfmt='%g', auto_format = True): 
    9090    """Print a list of lists as a table, so that columns line up nicely. 
    9191    header, if specified, will be printed as the first row. 
    9292    numfmt is the format for all numbers; you might want e.g. '%6.2f'. 
    9393    (If you want different formats in different columns, don't use 
    94     print_table.) sep is the separator between columns.""" 
     94    print_table.) sep is the separator between columns. 
     95     
     96    auto_format is a list specifying the formatting colours to use for each 
     97    row. The first element is the header colour, subsequent elements are 
     98    for alternating rows. eg. [ '^B', '^1', '^2' ] """ 
    9599    def ljust(s, size): 
    96100        return s + ' ' * (size - clen(s)) 
    97101    def rjust(s, size): 
    98102        return ' ' * (size - clen(s)) + s 
     103    if auto_format and type(auto_format) is not list: 
     104        auto_format = ['^B^U', '^6', '^B^6'] 
    99105    justs = [isnumber(x) and rjust or ljust for x in table[0]] 
    100106    if header: 
     
    104110    maxlen = lambda seq: max(map(clen, seq)) 
    105111    sizes = map(maxlen, zip(*[map(str, row) for row in table])) 
    106     rowalt = 0 
     112    rowalt = -1 
    107113    for row in table: 
     114        fmt = '' 
     115        if auto_format: 
     116            if rowalt == -1: 
     117                fmt = auto_format[0] 
     118                auto_format.pop(0) 
     119            else: 
     120                fmt = auto_format[rowalt % len(auto_format)] 
     121        cwrite(sys.stdout, fmt) 
    108122        for (j, size, x) in zip(justs, sizes, row): 
    109123            cwrite(sys.stdout, j(x, size), sep) 
    110         print 
     124        cprint('^N') 
     125        rowalt += 1