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

Source Code for Module PyFoam.Basics.TerminalFormatter

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