diff --git a/src/you_get/util/color/color.py b/src/you_get/util/color/color.py new file mode 100644 index 00000000..7888aee8 --- /dev/null +++ b/src/you_get/util/color/color.py @@ -0,0 +1,148 @@ +# -*- Coding:utf-8 -*- +#!/usr/bin/env python3 +""" +################################################################################ +Establish a Unified Color print across all kinds of os +################################################################################ +""" + +from .__init__ import iswin,islinux + +if islinux(): + from . import color_sh as sh +elif iswin(): + from . import color_cmd as cmd + + +def printWhite(obj): + if iswin(): + with cmd.printWhite(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'white')) + + else: + print(obj) +def printDarkPink(obj): + if iswin(): + with cmd.printDarkPink(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'purple')) + + else: + print(obj) + +def printBlue(obj): + """ + Belive it ,It's an ugly print-color. + Blue makes you blue :( + :param obj: + :return: + """ + if iswin(): + with cmd.printBlue(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'blue')) + + else: + print(obj) + + +def printDarkRed(obj): + if iswin(): + with cmd.printDarkRed(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'red')) + + else: + print(obj) + + +def printDarkSkyBlue(obj): + if iswin(): + with cmd.printDarkSkyBlue(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'cyan')) + + else: + print(obj) + +def printDarkGreen(obj): + if iswin(): + with cmd.printDarkGreen(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'green')) + + else: + print(obj) + +def printDarkYellow(obj): + if iswin(): + with cmd.printDarkYellow(): + print(obj) + elif islinux(): + print(sh.UseStyle(obj,'yellow')) + + else: + print(obj) + +################################################################################ +# Application layer encapsulation +# +# 4 Kind of Print: +# +# print Common Information : print_info +# print Succeed Information : print_ok +# print Warinng Information : print_warn or print_warning +# print Error Information : print_err or print_error +# +#You can also configure them with color_dict and print_map +################################################################################ +color_dict={'green' : printDarkGreen, + 'skyblue' : printDarkSkyBlue, + 'yellow' : printDarkYellow, + 'red' : printDarkRed, + 'blue':printBlue, + 'white':printWhite, + 'purple':printDarkPink} + +print_map={'info' : color_dict['skyblue'], + 'ok' : color_dict['green'], + 'warning' : color_dict['yellow'], + 'error' : color_dict['red']} + +def print_info(obj):print_map['info'](obj) +def print_ok(obj):print_map['ok'](obj) + +def print_warning(obj):print_map['warning'](obj) +def print_warn(obj): print_warning(obj) # print_warning is too long + +def print_error(obj):print_map['error'](obj) +def print_err(obj):print_error(obj) #print_error is too long + + + +if __name__ == '__main__': + print('isLinux?%r'%islinux()) + print('isWindows?%r'%iswin()) + + printDarkRed({'a':1,'b':2}) + printDarkGreen([1,2,3]) + printDarkSkyBlue((1,2,3)) + printDarkYellow({1,2,3}) + + printDarkPink('abc'+'def') + printBlue(printBlue) + printWhite('White Char') + + print_info('info') + print_ok('ok means that exec action succeed') + print_warning('warning') + print_warning('print_warning is too long') + print_error('error') + print_err('print_error is too long') diff --git a/src/you_get/util/color/color_cmd.py b/src/you_get/util/color/color_cmd.py new file mode 100644 index 00000000..cd7dae4d --- /dev/null +++ b/src/you_get/util/color/color_cmd.py @@ -0,0 +1,266 @@ +# -*- Coding:utf-8 -*- +#!/usr/bin/env python3 +from __future__ import print_function +""" +################################################################################ +Only for Windows CMD 、PowerShell + +Can be compatibale with python2(>2.6)(Care: UniocoedEncodeError) +################################################################################ +""" + +import ctypes +import sys + +STD_INPUT_HANDLE = -10 +STD_OUTPUT_HANDLE = -11 +STD_ERROR_HANDLE = -12 + +# 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色 +#由于该函数的限制,应该是只有这16种,可以前景色与背景色组合。也可以几种颜色通过或运算组合,组合后还是在这16种颜色中 + +# Windows CMD命令行 字体颜色定义 text colors +FOREGROUND_BLACK = 0x00 # black. +FOREGROUND_DARKBLUE = 0x01 # dark blue. +FOREGROUND_DARKGREEN = 0x02 # dark green. +FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue. +FOREGROUND_DARKRED = 0x04 # dark red. +FOREGROUND_DARKPINK = 0x05 # dark pink. +FOREGROUND_DARKYELLOW = 0x06 # dark yellow. +FOREGROUND_DARKWHITE = 0x07 # dark white. +FOREGROUND_DARKGRAY = 0x08 # dark gray. +FOREGROUND_BLUE = 0x09 # blue. +FOREGROUND_GREEN = 0x0a # green. +FOREGROUND_SKYBLUE = 0x0b # skyblue. +FOREGROUND_RED = 0x0c # red. +FOREGROUND_PINK = 0x0d # pink. +FOREGROUND_YELLOW = 0x0e # yellow. +FOREGROUND_WHITE = 0x0f # white. + + +# Windows CMD命令行 背景颜色定义 background colors +BACKGROUND_BLUE = 0x10 # dark blue. +BACKGROUND_GREEN = 0x20 # dark green. +BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue. +BACKGROUND_DARKRED = 0x40 # dark red. +BACKGROUND_DARKPINK = 0x50 # dark pink. +BACKGROUND_DARKYELLOW = 0x60 # dark yellow. +BACKGROUND_DARKWHITE = 0x70 # dark white. +BACKGROUND_DARKGRAY = 0x80 # dark gray. +BACKGROUND_BLUE = 0x90 # blue. +BACKGROUND_GREEN = 0xa0 # green. +BACKGROUND_SKYBLUE = 0xb0 # skyblue. +BACKGROUND_RED = 0xc0 # red. +BACKGROUND_PINK = 0xd0 # pink. +BACKGROUND_YELLOW = 0xe0 # yellow. +BACKGROUND_WHITE = 0xf0 # white. + + + +# get handle +std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) + +def set_cmd_text_color(color, handle=std_out_handle): + Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) + return Bool + +#reset white +def resetColor(): + set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) + +############################################################### + +class printColor: + """ + __exit__:self, exc_type, exc_value, traceback + """ + def __exit__(self,*args,**kwargs): + resetColor() + +#暗蓝色 +#dark blue +class printDarkBlue(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKBLUE) + + +#暗绿色 +#dark green +class printDarkGreen(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKGREEN) + + +#暗天蓝色 +#dark sky blue +class printDarkSkyBlue(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKSKYBLUE) + + +#暗红色 +#dark red +class printDarkRed(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKRED) + + +#暗粉红色 +#dark pink +class printDarkPink(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKPINK) + + +#暗黄色 +#dark yellow +class printDarkYellow(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKYELLOW) + + +#暗白色 +#dark white +class printDarkWhite(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKWHITE) + + +#暗灰色 +#dark gray +class printDarkGray(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_DARKGRAY) + + +#蓝色 +#blue +class printBlue(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_BLUE) + + +#绿色 +#green +class printGreen(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_GREEN) + + +#天蓝色 +#sky blue +class printSkyBlue(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_SKYBLUE) + + +#红色 +#red +class printRed(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_RED) + + +#粉红色 +#pink +class printPink(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_PINK) + + +#黄色 +#yellow +class printYellow(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_YELLOW) + + +#白色 +#white +class printWhite(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_WHITE) + + +################################################## + +#白底黑字 +#white bkground and black text +class printWhiteBlack(printColor): + def __enter__(self): + set_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE) + + +#白底黑字 +#white bkground and black text +class printWhiteBlack_2(printColor): + def __enter__(self): + set_cmd_text_color(0xf0) + + + +#黄底蓝字 +#white bkground and black text +class printYellowRed(printColor): + def __enter__(self): + set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED) + + + +############################################################## + +if __name__ == '__main__': + + with printDarkBlue(): + print(u'printDarkBlue:暗蓝色文字\n') + + with printDarkGreen(): + print(u'printDarkGreen:暗绿色文字\n') + + with printDarkSkyBlue(): + print(u'printDarkSkyBlue:暗天蓝色文字\n') + + with printDarkRed(): + print(u'printDarkRed:暗红色文字\n') + + with printDarkPink(): + print(u'printDarkPink:暗粉红色文字\n') + + with printDarkYellow(): + print(u'printDarkYellow:暗黄色文字\n') + + with printDarkWhite(): + print(u'printDarkWhite:暗白色文字\n') + + with printDarkGray(): + print(u'printDarkGray:暗灰色文字\n') + + with printBlue(): + print(u'printBlue:蓝色文字\n') + + with printGreen(): + print(u'printGreen:绿色文字\n') + + with printSkyBlue(): + print(u'printSkyBlue:天蓝色文字\n') + + with printRed(): + print(u'printRed:红色文字\n') + + with printPink(): + print(u'printPink:粉红色文字\n') + + with printYellow(): + print(u'printYellow:黄色文字\n') + + with printWhite(): + print(u'printWhite:白色文字\n') + + with printWhiteBlack(): + print(u'printWhiteBlack:白底黑字输出\n') + + with printWhiteBlack_2(): + print(u'printWhiteBlack_2:白底黑字输出(直接传入16进制参数)\n') + + with printYellowRed(): + print(u'printYellowRed:黄底红字输出\n') diff --git a/src/you_get/util/color/color_sh.py b/src/you_get/util/color/color_sh.py new file mode 100644 index 00000000..a03b3f99 --- /dev/null +++ b/src/you_get/util/color/color_sh.py @@ -0,0 +1,140 @@ +#/usr/bin/python +#-*- coding: utf-8 -*- +from __future__ import print_function + +""" +################################################################################ +Support for sh、bash (include cmd in win10) etc +Do not Support powershell + +Can be compatibale with python2(>2.6)(Care: UniocoedEncodeError) +################################################################################ +""" +# 格式:\033[显示方式;前景色;背景色m +# 说明: +# +# 前景色 背景色 颜色 +# --------------------------------------- +# 30 40 黑色 +# 31 41 红色 +# 32 42 绿色 +# 33 43 黃色 +# 34 44 蓝色 +# 35 45 紫红色 +# 36 46 青蓝色 +# 37 47 白色 +# +# 显示方式 意义 +# ------------------------- +# 0 终端默认设置 +# 1 高亮显示 +# 4 使用下划线 +# 5 闪烁 +# 7 反白显示 +# 8 不可见 +# +# 例子: +# \033[1;31;40m +# \033[0m ]]] + + +STYLE = { + 'fore': + { # 前景色 + 'black' : 30, # 黑色 + 'red' : 31, # 红色 + 'green' : 32, # 绿色 + 'yellow' : 33, # 黄色 + 'blue' : 34, # 蓝色 + 'purple' : 35, # 紫红色 + 'cyan' : 36, # 青蓝色 + 'white' : 37, # 白色 + }, + + 'back' : + { # 背景 + 'black' : 40, # 黑色 + 'red' : 41, # 红色 + 'green' : 42, # 绿色 + 'yellow' : 43, # 黄色 + 'blue' : 44, # 蓝色 + 'purple' : 45, # 紫红色 + 'cyan' : 46, # 青蓝色 + 'white' : 47, # 白色 + }, + + 'mode' : + { # 显示模式 + 'mormal' : 0, # 终端默认设置 + 'bold' : 1, # 高亮显示 + 'underline' : 4, # 使用下划线 + 'blink' : 5, # 闪烁 + 'invert' : 7, # 反白显示 + 'hide' : 8, # 不可见 + }, + + 'default' : + { + 'end' : 0, + }, +} + + +def UseStyle(obj, fore = '', mode = '', back = ''): + + mode = '%s' % STYLE['mode'][mode] if mode in STYLE['mode'] else '' + + fore = '%s' % STYLE['fore'][fore] if fore in STYLE['fore'] else '' + + back = '%s' % STYLE['back'][back] if back in STYLE['back'] else '' + + style = ';'.join([s for s in [mode, fore, back] if s]) + + style = '\033[%sm' % style if style else '' + + end = '\033[%sm' % STYLE['default']['end'] if style else '' + + return '%s%s%s' % (style, repr(obj), end) + + + +def TestColor( ): + + print((UseStyle('正常显示'))) + print ('') + + print ("测试显示模式") + print((UseStyle('高亮', mode = 'bold')),end=' ') + print((UseStyle('下划线', mode = 'underline')),end=' ') + print((UseStyle('闪烁', mode = 'blink')),end=' ') + print((UseStyle('反白', mode = 'invert')),end=' ') + print((UseStyle('不可见', mode = 'hide')),end=' ') + print('') + + + print("测试前景色") + print(UseStyle('黑色', fore = 'black'), end=' ') + print(UseStyle('红色', fore = 'red'), end=' ') + print(UseStyle('绿色', fore = 'green'), end=' ') + print(UseStyle('黄色', fore = 'yellow'), end=' ') + print(UseStyle('蓝色', fore = 'blue'), end=' ') + print(UseStyle('紫红色', fore = 'purple'), end=' ') + print(UseStyle('青蓝色', fore = 'cyan'), end=' ') + print(UseStyle('白色', fore = 'white')) + print('') + + + print("测试背景色") + print(UseStyle('黑色', back = 'black'), end=' ') + print(UseStyle('红色', back = 'red'), end=' ') + print(UseStyle('绿色', back = 'green'), end=' ') + print(UseStyle('黄色', back = 'yellow'), end=' ') + print(UseStyle('蓝色', back = 'blue'), end=' ') + print(UseStyle('紫红色', back = 'purple'), end=' ') + print(UseStyle('青蓝色', back = 'cyan'), end=' ') + print(UseStyle('白色', back = 'white')) + print('') +if __name__ == '__main__': + + TestColor() + print(UseStyle([1,2,3],fore='green'))