Changeset 272

Show
Ignore:
Timestamp:
10/03/05 01:43:22 (3 years ago)
Author:
athomas
Message:
  • Moved console related functions out of crash.util into crash.console
Files:

Legend:

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

    r269 r272  
    55from __future__ import generators 
    66import sys, re, operator, math, random, copy, sys,  os.path, bisect, os, textwrap 
    7  
    8 class ostream: 
    9     def __init__(self, file): 
    10         self.file = file 
    11  
    12     def __lshift__(self, obj): 
    13         obj = str(obj) 
    14         self.file.write(obj); 
    15         if obj == '\n': self.file.flush() 
    16         return self 
    17  
    18 cout = ostream(sys.stdout) 
    19 cerr = ostream(sys.stderr) 
    20 endl = '\n' 
    217 
    228class DelayedAbstractException: 
     
    669655    "Is x a sequence? We say it is if it has a __getitem__ method." 
    670656    return hasattr(x, '__getitem__') 
    671  
    672 def print_table(table, header=None, sep=' ', numfmt='%g'): 
    673     """Print a list of lists as a table, so that columns line up nicely. 
    674     header, if specified, will be printed as the first row. 
    675     numfmt is the format for all numbers; you might want e.g. '%6.2f'. 
    676     (If you want different formats in different columns, don't use 
    677     print_table.) sep is the separator between columns.""" 
    678     justs = [if_(isnumber(x), 'rjust', 'ljust') for x in table[0]] 
    679     if header: 
    680         table = [header] + table 
    681     table = [[if_(isnumber(x), lambda: numfmt % x, x)  for x in row] 
    682              for row in table] 
    683     maxlen = lambda seq: max(map(lambda txt: len(__cprint_strip.sub('', txt)), seq)) 
    684     sizes = map(maxlen, zip(*[map(str, row) for row in table])) 
    685     for row in table: 
    686         for (j, size, x) in zip(justs, sizes, row): 
    687             cwrite(sys.stdout, getattr(str(x), j)(size), sep) 
    688         print 
    689657 
    690658# Queues: Stack, FIFOQueue, PriorityQueue 
     
    748716#           return self.A.pop()[1] 
    749717 
    750 __cprint_re = re.compile(r'[^^]+|\^([N0-7BU])') 
    751 __cprint_strip = re.compile('\^([N0-7BU])') 
    752 __colour_terminal = 0 
    753 try: 
    754     import curses 
    755     curses.setupterm() 
    756     if curses.tigetnum('colors') >= 0: 
    757         __colour_terminal = 1 
    758 except ImportError: 
    759     pass 
    760  
    761 if sys.stdout.isatty() and __colour_terminal: 
    762     def cwrite(io, *args): 
    763         """ Print using colour escape codes similar to the Quake engine. That is, 
    764         ^0-7 correspond to colours, ^B toggles bold, ^U toggles underline and 
    765         ^N is reset to normal text. Colour is not automatically reset at the 
    766         end of output. """ 
    767         text = ' '.join(map(str, args)) 
    768         bold = underline = 0 
    769         out = [] 
    770         for token in __cprint_re.finditer(text): 
    771             if token.group(1): 
    772                 c = token.group(1) 
    773                 if c == 'B': 
    774                     if bold: 
    775                         out.append("") 
    776                     else: 
    777                         out.append("") 
    778                     bold = not bold 
    779                 elif c == 'U': 
    780                     if underline: 
    781                         out.append("") 
    782                     else: 
    783                         out.append("") 
    784                     underline = not underline 
    785                 elif c == 'N': 
    786                     underline = bold = 0 
    787                     out.append("") 
    788                 elif c >= '0' and c <= '7': 
    789                     out.append("[3" + c + "m") 
    790                 else: 
    791                     out.append(token.group(0)) 
    792             else: 
    793                 out.append(token.group(0)) 
    794         io.write(''.join(out)) 
    795  
    796 else: 
    797     def cwrite(io, *args): 
    798         io.write(__cprint_strip.sub('', ' '.join(map(str, args)))) 
    799          
    800 def cprint(*args): 
    801     cwrite(sys.stdout, *(args + ('\n',))) 
    802  
    803 def cprintstrip(*args): 
    804     return __cprint_strip.sub('', ' '.join(map(str, args))) 
    805  
    806 def clen(arg): 
    807     return len(cprintstrip(arg)) 
    808  
    809 def error(*args): 
    810     """ Print a red error message prefixed by ERR. """ 
    811     cprint("^1^BERR " + ' '.join(map(str, args)) + '^N') 
    812  
    813 def fatal(*args): 
    814     """ Print a red error message prefixed by FTL, then exit with status -1 """ 
    815     cprint("^1^BFTL " + ' '.join(map(str, args)) + '^N') 
    816     sys.exit(-1) 
    817  
    818 def warning(*args): 
    819     """ Print a yellow warning message prefixed by WRN """ 
    820     cprint("^3^BWRN " + ' '.join(map(str, args)) + '^N') 
    821  
    822 def info(*args): 
    823     """ Print a green notice prefixed by INF. """ 
    824     cprint("^2^BINF ^B" + ' '.join(map(str, args)) + '^N') 
    825  
    826718def tolist(o): 
    827719    """ Convert a scalar object into a list or simply return the list/tuple. """ 
     
    851743        return (os.EX_OK, os.popen(command + " 2>&1").readlines()) 
    852744 
    853 def termwidth(): 
    854     """ Find the current terminal width """ 
    855     return 'COLUMNS' in os.environ and int(os.environ['COLUMNS']) or 80 
    856  
    857 def wraptoterm(text, **argd): 
    858     """ Wrap the given text to the current terminal width """ 
    859     return '\n'.join(textwrap.wrap(text, termwidth(), **argd)) 
    860  
    861745def replace(seq, old, new): 
    862746    """ Replace all instances of old in seq with new """