Package PyFoam :: Package Basics :: Module TerminalFormatter
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.TerminalFormatter

 1  #  ICE Revision: $Id$  
 2  """Formats the output on a terminal""" 
 3   
 4  import os 
 5   
 6  from PyFoam.Infrastructure.Configuration import Configuration as config 
 7   
8 -def getTerminalCode(code):
9 result="" 10 try: 11 result=os.popen("tput "+code).read() 12 except: 13 pass 14 return result
15
16 -class TerminalFormatter(object):
17 """Class that contains the formating codes for the terminal""" 18 19 reset =getTerminalCode("sgr0") 20 21 bold =getTerminalCode("bold") 22 under =getTerminalCode("smul") 23 standout=getTerminalCode("smso") 24 25 black =getTerminalCode("setaf 0") 26 red =getTerminalCode("setaf 1") 27 green =getTerminalCode("setaf 2") 28 cyan =getTerminalCode("setaf 3") 29 blue =getTerminalCode("setaf 4") 30 magenta =getTerminalCode("setaf 5") 31 yellow =getTerminalCode("setaf 6") 32 white =getTerminalCode("setaf 7") 33 34 back_black =getTerminalCode("setab 0") 35 back_red =getTerminalCode("setab 1") 36 back_green =getTerminalCode("setab 2") 37 back_cyan =getTerminalCode("setab 3") 38 back_blue =getTerminalCode("setab 4") 39 back_magenta =getTerminalCode("setab 5") 40 back_yellow =getTerminalCode("setab 6") 41 back_white =getTerminalCode("setab 7") 42
43 - def buildSequence(self,specification):
44 """Build an escape sequence from a specification string 45 @param specification: the specification string that is a number 46 of komma-separated words. The words specify the color and the 47 formatting""" 48 49 seq="" 50 for s in specification.split(','): 51 seq+=eval("self."+s) 52 53 return seq
54
55 - def addFormat(self,name,specification):
56 """Add a new format to the object 57 @param name: Name under which the format is added to the formatter 58 @param specification: The specification string for the format""" 59 60 exec("self."+name+"=self.buildSequence('"+specification+"')")
61
62 - def getConfigFormat(self,name,shortName=None):
63 """Gets a format sequence from the global configuration and adds it 64 to the formatter object 65 @param name: Name under which this is found in the 'Formats'-section 66 of the configuration 67 @param shortName: Short name under which this is stored in the 68 foratter. If none is given the regular name is used""" 69 70 spec=config().get("Formats",name,default="reset") 71 nm=name 72 if shortName: 73 nm=shortName 74 self.addFormat(nm,spec)
75