MCPcopy
hub / github.com/django/django / colorize

Function colorize

django/utils/termcolors.py:19–61  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

17
18
19def 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
64def make_style(opts=(), **kwargs):

Callers 5

test_colorize_resetMethod · 0.90
test_colorize_fg_bgMethod · 0.90
test_colorize_optsMethod · 0.90
make_styleFunction · 0.85

Calls 3

itemsMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by 4

test_colorize_resetMethod · 0.72
test_colorize_fg_bgMethod · 0.72
test_colorize_optsMethod · 0.72