首页 > 代码库 > Linux&Dos终端彩色打印

Linux&Dos终端彩色打印

#! /usr/bin/env python
# -*- coding: utf-8 -*-

################################################################################
#
# Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
#
################################################################################

"""
Windows命令行彩色输出
@authors    :   guoxiaohui01<guoxiaohui01@baidu.com>
@copyright  :   baidu
@date       :   2014-10-09
@version    :   1.0.0.0
"""

import ctypes

class CWinColor(object):
    ‘‘‘
    Windows平台彩色输出
    ‘‘‘    
    @staticmethod
    def color_print(print_text, color_type):
        """
        彩色打印 (红)+(绿)=(黄)(蓝)+(绿)=(青)(红)+(蓝)=(品红)
        输入:
            color_type: red|green|blue|yellow|cyan|pink
        """
        STD_INPUT_HANDLE = -10
        STD_OUTPUT_HANDLE= -11
        STD_ERROR_HANDLE = -12

        #前景字体
        FOREGROUND_BLUE = 0x01
        FOREGROUND_GREEN = 0x02
        FOREGROUND_RED = 0x04
        FOREGROUND_INTENSITY = 0x08

        #背景
        BACKGROUND_BLUE = 0x10
        BACKGROUND_GREEN= 0x20
        BACKGROUND_RED = 0x40 
        BACKGROUND_INTENSITY = 0x80

        color_red = FOREGROUND_RED
        color_green = FOREGROUND_GREEN
        color_blue = FOREGROUND_BLUE

        color_yellow = FOREGROUND_GREEN | FOREGROUND_RED 
        color_cyan = FOREGROUND_BLUE | FOREGROUND_GREEN
        color_pink = FOREGROUND_RED | FOREGROUND_BLUE

        intensity = FOREGROUND_INTENSITY

        std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
        
        if (color_type == "red"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_red | intensity)
        if (color_type == "green"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_green | intensity)
        if (color_type == "blue"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_blue | intensity)        

        if (color_type == "yellow"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_yellow | intensity)
        if (color_type == "cyan"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_cyan | intensity)
        if (color_type == "pink"):
            ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_pink | intensity)

        print print_text

        ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color_red | color_blue | color_green)


def demo():
    """
    测试用例
    """
    CWinColor.color_print("这是红色".decode("UTF8").encode("GBK"), "red")
    CWinColor.color_print("这是绿色".decode("UTF8").encode("GBK"), "green")
    CWinColor.color_print("这是蓝色".decode("UTF8").encode("GBK"), "blue")
    CWinColor.color_print("这是黄色".decode("UTF8").encode("GBK"), "yellow")
    CWinColor.color_print("这是粉色".decode("UTF8").encode("GBK"), "pink")

if __name__ == "__main__":
    demo()

实际使用时,如果要兼容Linux的彩色打印,可这样:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys

if sys.platform.startswith("win"):
    import win_color
    print_red = lambda (input):win_color.CWinColor.color_print(input, "red")
    print_green = lambda (input):win_color.CWinColor.color_print(input, "green")
    print_blue = lambda (input):win_color.CWinColor.color_print(input, "blue") 
    print_yellow = lambda (input):win_color.CWinColor.color_print(input, "yellow")
    print_cyan = lambda (input):win_color.CWinColor.color_print(input, "cyan") 
    print_pink = lambda (input):win_color.CWinColor.color_print(input, "pink") 
else:
    import termcolor
    print_red = lambda (input):termcolor.cprint(input, ‘red‘, attrs=[‘bold‘])
    print_blue = lambda (input):termcolor.cprint(input, ‘blue‘, attrs=[‘bold‘]) 
    print_green = lambda (input):termcolor.cprint(input, ‘green‘, attrs=[‘bold‘])
    print_yellow = lambda (input):termcolor.cprint(input, ‘yellow‘, attrs=[‘bold‘])
    print_cyan = lambda (input):termcolor.cprint(input, ‘cyan‘, attrs=[‘bold‘]) 
    print_pink = lambda (input):termcolor.cprint(input, ‘magenta‘, attrs=[‘bold‘])

print_pink("pink")
print_green("green")
print_yellow("yellow")
print_red("red")
print_cyan("cyan")
print_blue("blue")

效果如下:

  




Linux&Dos终端彩色打印