Package rocon_utilities :: Module console
[frames] | no frames]

Source Code for Module rocon_utilities.console

  1  #!/usr/bin/env python 
  2  # 
  3  # License: BSD 
  4  #   https://raw.github.com/robotics-in-concert/rocon_multimaster/master/rocon_utilities/LICENSE 
  5  # 
  6   
  7  ############################################################################## 
  8  # Imports 
  9  ############################################################################## 
 10   
 11  import sys 
 12   
 13  ############################################################################## 
 14  # Methods 
 15  ############################################################################## 
 16   
 17   
18 -def console_has_colours(stream):
19 if not hasattr(stream, "isatty"): 20 return False 21 if not stream.isatty(): 22 return False # auto color only on TTYs 23 try: 24 import curses 25 curses.setupterm() 26 return curses.tigetnum("colors") > 2 27 except: 28 # guess false in case of error 29 return False
30 31 has_colours = console_has_colours(sys.stdout) 32 if has_colours: 33 #reset = "\x1b[0;0m" 34 reset = "\x1b[0m" 35 bold = "\x1b[%sm" % '1' 36 black, red, green, yellow, blue, magenta, cyan, white = ["\x1b[%sm" % str(i) for i in range(30, 38)] 37 bold_black, bold_red, bold_green, bold_yellow, bold_blue, bold_magenta, bold_cyan, bold_white = ["\x1b[%sm" % ('1;' + str(i)) for i in range(30, 38)] 38 else: 39 reset = "" 40 bold = "" 41 black, red, green, yellow, blue, magenta, cyan, white = ["" for i in range(30, 38)] 42 bold_black, bold_red, bold_green, bold_yellow, bold_blue, bold_magenta, bold_cyan, bold_white = ["" for i in range(30, 38)] 43 44 colours = [ 45 bold, 46 black, red, green, yellow, blue, magenta, cyan, white, 47 bold_black, bold_red, bold_green, bold_yellow, bold_blue, bold_magenta, bold_cyan, bold_white 48 ] 49 50
51 -def pretty_print(msg, colour=white):
52 if has_colours: 53 seq = colour + msg + reset 54 sys.stdout.write(seq) 55 else: 56 sys.stdout.write(msg)
57 58
59 -def pretty_println(msg, colour=white):
60 if has_colours: 61 seq = colour + msg + reset 62 sys.stdout.write(seq) 63 sys.stdout.write("\n") 64 else: 65 sys.stdout.write(msg)
66 67 68 ############################################################################## 69 # Console 70 ############################################################################## 71 72
73 -def debug(msg):
74 pretty_print("%s\n" % msg, green)
75 76
77 -def warning(msg):
78 pretty_print("%s\n" % msg, yellow)
79 80
81 -def error(msg):
82 pretty_print("%s\n" % msg, red)
83 84
85 -def logdebug(message):
86 pretty_print("[debug] " + message + "\n", green)
87 88
89 -def logwarn(message):
90 pretty_print("[warning] " + message + "\n", yellow)
91 92
93 -def logerror(message):
94 pretty_print("[error] " + message + "\n", red)
95 96
97 -def logfatal(message):
98 pretty_print("[error] " + message + "\n", bold_red)
99 100 101 ############################################################################## 102 # Main 103 ############################################################################## 104 105 if __name__ == '__main__': 106 for colour in colours: 107 pretty_print("dude\n", colour) 108 logdebug("info message") 109 logwarn("warning message") 110 logerror("error message") 111 logfatal("fatal message") 112 pretty_print("red\n", red) 113 print("some normal text") 114