1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.console |
|
4 ~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Format colored console output. |
|
7 |
|
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 esc = "\x1b[" |
|
13 |
|
14 codes = {} |
|
15 codes[""] = "" |
|
16 codes["reset"] = esc + "39;49;00m" |
|
17 |
|
18 codes["bold"] = esc + "01m" |
|
19 codes["faint"] = esc + "02m" |
|
20 codes["standout"] = esc + "03m" |
|
21 codes["underline"] = esc + "04m" |
|
22 codes["blink"] = esc + "05m" |
|
23 codes["overline"] = esc + "06m" |
|
24 |
|
25 dark_colors = ["black", "red", "green", "yellow", "blue", |
|
26 "magenta", "cyan", "gray"] |
|
27 light_colors = ["brightblack", "brightred", "brightgreen", "brightyellow", "brightblue", |
|
28 "brightmagenta", "brightcyan", "white"] |
|
29 |
|
30 x = 30 |
|
31 for d, l in zip(dark_colors, light_colors): |
|
32 codes[d] = esc + "%im" % x |
|
33 codes[l] = esc + "%im" % (60 + x) |
|
34 x += 1 |
|
35 |
|
36 del d, l, x |
|
37 |
|
38 codes["white"] = codes["bold"] |
|
39 |
|
40 |
|
41 def reset_color(): |
|
42 return codes["reset"] |
|
43 |
|
44 |
|
45 def colorize(color_key, text): |
|
46 return codes[color_key] + text + codes["reset"] |
|
47 |
|
48 |
|
49 def ansiformat(attr, text): |
|
50 """ |
|
51 Format ``text`` with a color and/or some attributes:: |
|
52 |
|
53 color normal color |
|
54 *color* bold color |
|
55 _color_ underlined color |
|
56 +color+ blinking color |
|
57 """ |
|
58 result = [] |
|
59 if attr[:1] == attr[-1:] == '+': |
|
60 result.append(codes['blink']) |
|
61 attr = attr[1:-1] |
|
62 if attr[:1] == attr[-1:] == '*': |
|
63 result.append(codes['bold']) |
|
64 attr = attr[1:-1] |
|
65 if attr[:1] == attr[-1:] == '_': |
|
66 result.append(codes['underline']) |
|
67 attr = attr[1:-1] |
|
68 result.append(codes[attr]) |
|
69 result.append(text) |
|
70 result.append(codes['reset']) |
|
71 return ''.join(result) |
|