Python

7-命令行颜色输出

2019-01-16  本文已影响0人  神秘加冰

Mac 下代码颜色

ESC [ 0 m       # reset all (colors and brightness)
ESC [ 1 m       # bright
ESC [ 2 m       # dim (looks same as normal brightness)
ESC [ 22 m      # normal brightness

# FOREGROUND:
ESC [ 30 m      # black
ESC [ 31 m      # red
ESC [ 32 m      # green
ESC [ 33 m      # yellow
ESC [ 34 m      # blue
ESC [ 35 m      # magenta
ESC [ 36 m      # cyan
ESC [ 37 m      # white
ESC [ 39 m      # reset

# BACKGROUND
ESC [ 40 m      # black
ESC [ 41 m      # red
ESC [ 42 m      # green
ESC [ 43 m      # yellow
ESC [ 44 m      # blue
ESC [ 45 m      # magenta
ESC [ 46 m      # cyan
ESC [ 47 m      # white
ESC [ 49 m      # reset

命令行输出上色

# 渲染
if os.name == 'nt':
    FOREGROUND_WHITE = 0x0007
    FOREGROUND_BLUE = 0x01 # text color contains blue.
    FOREGROUND_GREEN= 0x02 # text color contains green.
    FOREGROUND_RED  = 0x04 # text color contains red.
    FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN
    STD_OUTPUT_HANDLE= -11
    std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    def set_color(color, handle=std_out_handle):
        bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
        return bool
else:
    FOREGROUND_WHITE = '\033[37m'
    FOREGROUND_GREEN= '\033[32m' # text color contains green.
    FOREGROUND_RED  = '\033[31m' # text color contains red.
    def set_color(color):
        return color

def succeed_output(msg):
    print(set_color(FOREGROUND_GREEN),msg,set_color(FOREGROUND_WHITE))
    
def failed_output(msg):
    print(set_color(FOREGROUND_RED),msg,set_color(FOREGROUND_WHITE))

上一篇下一篇

猜你喜欢

热点阅读