python带颜色打印字符串

注意
本文最后更新于 2021-03-28,文中内容可能已过时。

之前调试pwn题的时候,有时候需要将某些特别的,重要的信息用不一样的颜色打印出来。查阅一些资料,了解了print函数的特性后,自己写了一个脚本,可以用来获取带颜色信息的字符串或者打印一串带颜色、背景色、下划线等的字符串。

脚本内容

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    : print_with_color.py
@Time    : 2021/03/07 12:41:35
@Author  : Lynne
@Email   : ch22166@163.com
@Desc    : None
'''
from functools import partial

class FontColor:
    BLACK = 30
    RED = 31
    GREEN = 32
    YELLO = 33
    BLUE = 34
    AMARANTH = 35
    CYAN = 36
    WHITE = 37
    
class BackgroundColor:
    NOCOLOR = -1
    BLACK = 40
    RED = 41
    GREEN = 42
    YELLO = 43
    BLUE = 44
    AMARANTH = 45
    CYAN = 46
    WHITE = 47
    
class TerminalMode:
    DEFAULT = 0
    HIGHLIGHT = 1
    UNDERLINE = 4
    TWINKLE = 5
    ANTI_WHITE = 7
    INVISIBLE = 8
    

def __check(font_color:int, background_color:int, terminal_mode:int) -> bool:
    b1 = (font_color >= FontColor.BLACK and font_color <= FontColor.WHITE)
    b2 = (background_color >= BackgroundColor.BLACK and background_color <= BackgroundColor.WHITE) or background_color == BackgroundColor.NOCOLOR
    b3 = (terminal_mode >= TerminalMode.DEFAULT and terminal_mode <= TerminalMode.INVISIBLE and terminal_mode != 2 and terminal_mode != 3 and terminal_mode != 6)
    return (b1 and b2 and b3)


def get_str_with_color(print_str:str, *,
                       font_color:int=FontColor.WHITE, 
                       background_color:int=BackgroundColor.NOCOLOR, 
                       terminal_mode:int=TerminalMode.DEFAULT)-> str:
    """Decorate a string with color

    Args:
        print_str (str): The str you want to modify.
        font_color (int, optional): Font color. Defaults to FontColor.WHITE.
        background_color (int, optional): Background color. Defaults to BackgroundColor.NOCOLOR.
        terminal_mode (int, optional): terminal mode. Defaults to TerminalMode.DEFAULT.

    Returns:
        str: A string with elaborate decoration.
    """
    check = __check(font_color, background_color, terminal_mode)
    if not check:
        print('\033[1;31;47mWARNING: Failure to set color!\033[0m')
        return print_str
    if background_color == BackgroundColor.NOCOLOR:
        background_color = ''
    else:
        background_color = ';'+str(background_color)
    res_str = '\033[{};{}{}m{}\033[0m'.format(terminal_mode, font_color, background_color, print_str)
    return res_str


def print_color(print_str:str, *,
                font_color:int=FontColor.WHITE, 
                background_color:int=BackgroundColor.NOCOLOR, 
                terminal_mode:int=TerminalMode.DEFAULT):
    """print a string with color

    Args:
        print_str (str): The str you want to modify.
        font_color (int, optional): Font color. Defaults to FontColor.WHITE.
        background_color (int, optional): Background color. Defaults to BackgroundColor.NOCOLOR.
        terminal_mode (int, optional): terminal mode. Defaults to TerminalMode.DEFAULT.

    """
    print(get_str_with_color(print_str, font_color=font_color, background_color=background_color, terminal_mode=terminal_mode))
    

# make rgb print func
print_red = partial(print_color, 
                    font_color=FontColor.RED, 
                    background_color=BackgroundColor.NOCOLOR, 
                    terminal_mode=TerminalMode.DEFAULT)

print_green = partial(print_color, 
                    font_color=FontColor.GREEN, 
                    background_color=BackgroundColor.NOCOLOR, 
                    terminal_mode=TerminalMode.DEFAULT)

print_blue = partial(print_color, 
                    font_color=FontColor.BLUE, 
                    background_color=BackgroundColor.NOCOLOR, 
                    terminal_mode=TerminalMode.DEFAULT)

if __name__ == '__main__':
    print('Original print: lynne')
    print_red('Print with red font: lynne')
    print_green('Print with green font: lynne')
    print_blue('Print with blue font:lynne')
    print_color('Print with cyan font, blue background and underline: lynne', font_color=FontColor.CYAN, background_color=BackgroundColor.BLUE, terminal_mode=TerminalMode.UNDERLINE)

在控制台的打印效果如下: https://lynne-markdown.oss-cn-hangzhou.aliyuncs.com/img/20210307164058.png

使用

  • get_str_with_color:获取带颜色信息的字符串
  • print_color:带颜色打印字符串
  • print_red/print_green/print_blue:自已定义一些偏函数,方便使用
Buy me a coffee~
roderick 支付宝支付宝
roderick 微信微信
0%