Changeset 583
- Timestamp:
- 07/26/08 01:39:32 (4 months ago)
- Files:
-
- cly/trunk/cly/builder.py (modified) (1 diff)
- cly/trunk/cly/console.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/builder.py
r582 r583 514 514 Upcalling is necessary. 515 515 516 : namespace: The XML namespace of the attribute. Compare against516 :param namespace: The XML namespace of the attribute. Compare against 517 517 XMLGrammar.EVAL_NS to determine if forced evaluation 518 518 can/should occur. 519 : name: Attribute name.520 : value: Value to cast.521 522 :r eturns: Tuple of (value, options) where options is a dictionary of523 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. 524 524 """ 525 525 casts = { cly/trunk/cly/console.py
r570 r583 354 354 355 355 def 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 """ 357 360 try: 358 361 import curses 359 362 return curses.tigetnum('cols') 360 363 except: 361 return int(os.environ.get('COLUMNS', 80))364 return int(os.environ.get('COLUMNS', -1)) 362 365 363 366 364 367 def 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 """ 366 372 try: 367 373 import curses 368 374 return curses.tigetnum('lines') 369 375 except: 370 return int(os.environ.get('LINES', 25))376 return int(os.environ.get('LINES', -1)) 371 377 372 378 … … 405 411 if width is None: 406 412 width = termwidth() 413 if width == -1: 414 return [rtext] 407 415 out = [] 408 416 for text in rtext.splitlines(): … … 444 452 if width is None: 445 453 width = termwidth() 454 if width == -1: 455 return text 446 456 text = cwraptext(text, width) 447 457 out = '' … … 455 465 if width is None: 456 466 width = termwidth() 467 if width == -1: 468 return text 457 469 text = cwraptext(text, width) 458 470 out = '' … … 464 476 def print_table(header, table, sep=u' ', indent=u'', expand_to_fit=True, 465 477 header_format='^B^U', row_format=('^6', '^B^6'), 466 min_widths=None ):478 min_widths=None, term_width=None): 467 479 """Print a list of lists as a table, so that columns line up nicely. 468 480 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. 499 505 """ 500 506 def ctlen(s): … … 507 513 508 514 # 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) 510 521 if not isinstance(min_widths, dict): 511 522 min_widths = dict(enumerate(min_widths or []))
