目錄之前除錯pwn題的時候,有時候需要將某些特別的,重要的資訊用不一樣的顏色列印出來。查閱一些資料,了解了print
函式的特性後,自己寫了乙個指令碼,可以用來獲取帶顏色資訊的字串或者列印一串帶顏色、背景色、下劃線等的字串。
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''@file : print_with_color.py
@time : 2021/03/07 12:41:35
@author : lynne
@email : [email protected]
@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)
在控制台的列印效果如下:
c語言printf列印字串顏色
基本列印 格式 printf 033 字背景顏色 字型顏色m字串 033 0m printf 033 41 32m字型背景是紅色,字是綠色 033 0m n 41是字背景顏色,32是字型的顏色,字型背景是紅色,字是綠色是 要輸出的 字串.後面的 033 033 0m是 配對的為 控制碼。先來說一下顏...
DbgPrint列印字串
1 直接列印字串。dbgprint hello world 2 空結尾的字串,你可以用普通得c 語法表示字串常量 char variable string hello world dbgprint s variable string 3 空結尾的寬字串 wchar 型別 wchar string w...
Scala列印字串
1 字串,通過 號連線 2 printf用法 字串,通過 傳值。3 字串模板 通過 獲取變數值 列印字串 val username zhangdan val userage 20println 使用者名稱 username 使用者年齡 userage scala中簡化了關於json的表達 print...