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
| class Printer(object): DM = {‘default’: ‘00’, ‘bold’: ‘01’, ‘italic’: ‘03’, ‘underline’: ‘04’} FG = {‘default’: ‘39’, ‘black’: ‘30’, ‘red’: ‘31’, ‘green’: ‘32’, ‘yellow’: ‘33’, ‘blue’: ‘34’, ‘white’: ‘37’} BG = {‘default’: ‘49’, ‘black’: ‘40’, ‘red’: ‘41’, ‘green’: ‘42’, ‘yellow’: ‘43’, ‘blue’: ‘44’, ‘white’: ‘47’} DEFAULT = ‘\033[00;39;49m’ CUSTOM = ‘\033[{0};{1};{2}m’
def init(self): self.map = {} for dier in dir(self): if ‘custom’ in dier: lev = getattr(self, dier) self.map[lev[0]] = lev[1]
custom_info = (0, CUSTOM.format(DM[‘default’], FG[‘default’], BG[‘default’])) custom_success = (1, CUSTOM.format(DM[‘underline’], FG[‘default’], BG[‘green’])) custom_warn = (2, CUSTOM.format(DM[‘default’], FG[‘red’], BG[‘default’])) custom_error = (3, CUSTOM.format(DM[‘bold’], FG[‘red’], BG[‘default’]))
def printer(mode, chs): p = Printer() style = p.map[mode] print ‘%s%s%s’ % (style, chs, p.DEFAULT)
if name == ‘main‘: printer(0, “info…”) printer(1, “success…”) printer(2, “warn…”) printer(3, “error…”)
|