Read the current text color in a xterm

前端 未结 4 579
情话喂你
情话喂你 2021-01-30 02:43

I\'m writing various utilities, and I\'m really liking colorized text. Nothing fancy, just using escape sequences. I\'ve created a simple class that has a pprint(msg, color) fun

4条回答
  •  庸人自扰
    2021-01-30 03:24

    RED = 31
    GREEN = 32
    ESCAPE = '%s[' % chr(27)
    RESET = '%s0m' % ESCAPE
    FORMAT = '1;%dm'
    
    def colorize(text, color):
        return ESCAPE + (FORMAT % (color, )) + text + RESET
    

    This function will return a string that will print colorized, with the terminal automatically being reset afterwards.

提交回复
热议问题