Changeset 420
- Timestamp:
- 05/08/07 21:17:03 (2 years ago)
- Files:
-
- cly/trunk/cly/console.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/console.py
r417 r420 340 340 341 341 342 def print_table(header, table, sep=' ', auto_format=('^B^U', '^6', '^B^6'), expand_to_fit=1): 342 def print_table(header, table, sep=' ', indent='', 343 auto_format=('^B^U', '^6', '^B^6'), expand_to_fit=True): 343 344 """Print a list of lists as a table, so that columns line up nicely. 344 345 … … 351 352 Data to print. 352 353 353 ``sep=' '`` 354 ``sep=' '``: string 354 355 The column separator. 356 357 ``indent=''``: string 358 Table indentation as a string. 355 359 356 360 ``auto_format=('^B^U', '^6', '^B^2')``: tuple … … 359 363 alternating rows. 360 364 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. 364 369 365 370 Note: ``print_table`` supports the ``^R`` formatting code, in addition to … … 379 384 if i < cols - 1: 380 385 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)) 384 397 colwidths = [int(float(x) * scale) for x in colwidths] 385 398 mincol = min(colwidths) 386 399 for i, col in enumerate(colwidths): 387 400 if col == mincol: 388 colwidths[0] += termwidth()- sum(colwidths)401 colwidths[0] += max_width - sum(colwidths) 389 402 390 403 auto_format = list(auto_format) … … 414 427 # Emit 415 428 for i in range(0, realrows): 416 cwrite(sys.stdout, fmt)429 cwrite(sys.stdout, indent + fmt) 417 430 for j, col in enumerate(xrow): 418 431 slen = j < cols - 1 and seplen or 0
