Return your text, enclosed in ANSI graphics codes. Depends on the keyword arguments 'fg' and 'bg', and the contents of the opts tuple/list. Return the RESET code if no parameters are given. Valid colors: 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'w
(text="", opts=(), **kwargs)
| 17 | |
| 18 | |
| 19 | def colorize(text="", opts=(), **kwargs): |
| 20 | """ |
| 21 | Return your text, enclosed in ANSI graphics codes. |
| 22 | |
| 23 | Depends on the keyword arguments 'fg' and 'bg', and the contents of |
| 24 | the opts tuple/list. |
| 25 | |
| 26 | Return the RESET code if no parameters are given. |
| 27 | |
| 28 | Valid colors: |
| 29 | 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white' |
| 30 | |
| 31 | Valid options: |
| 32 | 'bold' |
| 33 | 'underscore' |
| 34 | 'blink' |
| 35 | 'reverse' |
| 36 | 'conceal' |
| 37 | 'noreset' - string will not be auto-terminated with the RESET code |
| 38 | |
| 39 | Examples: |
| 40 | colorize('hello', fg='red', bg='blue', opts=('blink',)) |
| 41 | colorize() |
| 42 | colorize('goodbye', opts=('underscore',)) |
| 43 | print(colorize('first line', fg='red', opts=('noreset',))) |
| 44 | print('this should be red too') |
| 45 | print(colorize('and so should this')) |
| 46 | print('this should not be red') |
| 47 | """ |
| 48 | code_list = [] |
| 49 | if text == "" and len(opts) == 1 and opts[0] == "reset": |
| 50 | return "\x1b[%sm" % RESET |
| 51 | for k, v in kwargs.items(): |
| 52 | if k == "fg": |
| 53 | code_list.append(foreground[v]) |
| 54 | elif k == "bg": |
| 55 | code_list.append(background[v]) |
| 56 | for o in opts: |
| 57 | if o in opt_dict: |
| 58 | code_list.append(opt_dict[o]) |
| 59 | if "noreset" not in opts: |
| 60 | text = "%s\x1b[%sm" % (text or "", RESET) |
| 61 | return "%s%s" % (("\x1b[%sm" % ";".join(code_list)), text or "") |
| 62 | |
| 63 | |
| 64 | def make_style(opts=(), **kwargs): |