michael@0: import sys michael@0: michael@0: class Terminal(object): michael@0: COLOR = { michael@0: 'red': '31', michael@0: 'green': '32', michael@0: 'blue': '34', michael@0: 'gray': '37' michael@0: } michael@0: NORMAL_INTENSITY = '1' michael@0: BRIGHT_INTENSITY = '2' michael@0: ESCAPE = '\x1b[' michael@0: RESET = '0' michael@0: SEPARATOR = ';' michael@0: COLOR_CODE = 'm' michael@0: CLEAR_RIGHT_CODE = 'K' michael@0: michael@0: @classmethod michael@0: def set_color(cls, color): michael@0: """ michael@0: color: str - color definition string michael@0: """ michael@0: mod = Terminal.NORMAL_INTENSITY michael@0: if color.startswith('bright'): michael@0: mod = Terminal.BRIGHT_INTENSITY michael@0: color = color[len('bright'):] michael@0: color_code = Terminal.COLOR[color] michael@0: michael@0: sys.stdout.write(cls.ESCAPE + color_code + cls.SEPARATOR + mod + cls.COLOR_CODE) michael@0: michael@0: @classmethod michael@0: def reset_color(cls): michael@0: sys.stdout.write(cls.ESCAPE + cls.RESET + cls.COLOR_CODE) michael@0: michael@0: @classmethod michael@0: def clear_right(cls): michael@0: sys.stdout.write(cls.ESCAPE + cls.CLEAR_RIGHT_CODE)