|
19 | 19 |
|
20 | 20 |
|
21 | 21 | # Returns escape codes from format codes |
22 | | -def esc(*x: str) -> str: |
23 | | - return "\033[" + ";".join(x) + "m" |
| 22 | +def esc(*codes: int) -> str: |
| 23 | + return "\033[" + ";".join(str(code) for code in codes) + "m" |
24 | 24 |
|
25 | 25 |
|
26 | | -# The initial list of escape codes |
27 | 26 | escape_codes = { |
28 | | - "reset": esc("0"), |
29 | | - "bold": esc("01"), |
30 | | - "thin": esc("02"), |
| 27 | + "reset": esc(0), |
| 28 | + "bold": esc(1), |
| 29 | + "thin": esc(2), |
31 | 30 | } |
32 | 31 |
|
33 | | -# The color names |
34 | | -COLORS = [ |
35 | | - "black", |
36 | | - "red", |
37 | | - "green", |
38 | | - "yellow", |
39 | | - "blue", |
40 | | - "purple", |
41 | | - "cyan", |
42 | | - "white", |
43 | | -] |
| 32 | +escape_codes_foreground = { |
| 33 | + "black": 30, |
| 34 | + "red": 31, |
| 35 | + "green": 32, |
| 36 | + "yellow": 33, |
| 37 | + "blue": 34, |
| 38 | + "purple": 35, |
| 39 | + "cyan": 36, |
| 40 | + "white": 37, |
| 41 | + "light_black": 90, |
| 42 | + "light_red": 91, |
| 43 | + "light_green": 92, |
| 44 | + "light_yellow": 93, |
| 45 | + "light_blue": 94, |
| 46 | + "light_purple": 95, |
| 47 | + "light_cyan": 96, |
| 48 | + "light_white": 97, |
| 49 | +} |
| 50 | + |
| 51 | +escape_codes_background = { |
| 52 | + "black": 40, |
| 53 | + "red": 41, |
| 54 | + "green": 42, |
| 55 | + "yellow": 43, |
| 56 | + "blue": 44, |
| 57 | + "purple": 45, |
| 58 | + "cyan": 46, |
| 59 | + "white": 47, |
| 60 | + "light_black": 100, |
| 61 | + "light_red": 101, |
| 62 | + "light_green": 102, |
| 63 | + "light_yellow": 103, |
| 64 | + "light_blue": 104, |
| 65 | + "light_purple": 105, |
| 66 | + "light_cyan": 106, |
| 67 | + "light_white": 107, |
| 68 | + # Bold background colors don't exist, |
| 69 | + # but we used to provide these names. |
| 70 | + "bold_black": 100, |
| 71 | + "bold_red": 101, |
| 72 | + "bold_green": 102, |
| 73 | + "bold_yellow": 103, |
| 74 | + "bold_blue": 104, |
| 75 | + "bold_purple": 105, |
| 76 | + "bold_cyan": 106, |
| 77 | + "bold_white": 107, |
| 78 | +} |
| 79 | + |
| 80 | +# Foreground without prefix |
| 81 | +for name, code in escape_codes_foreground.items(): |
| 82 | + escape_codes["%s" % name] = esc(code) |
| 83 | + escape_codes["bold_%s" % name] = esc(1, code) |
| 84 | + escape_codes["thin_%s" % name] = esc(2, code) |
44 | 85 |
|
45 | | -PREFIXES = [ |
46 | | - # Foreground without prefix |
47 | | - ("3", ""), |
48 | | - ("01;3", "bold_"), |
49 | | - ("02;3", "thin_"), |
50 | | - # Foreground with fg_ prefix |
51 | | - ("3", "fg_"), |
52 | | - ("01;3", "fg_bold_"), |
53 | | - ("02;3", "fg_thin_"), |
54 | | - # Background with bg_ prefix - bold/light works differently |
55 | | - ("4", "bg_"), |
56 | | - ("10", "bg_bold_"), |
57 | | -] |
| 86 | +# Foreground with fg_ prefix |
| 87 | +for name, code in escape_codes_foreground.items(): |
| 88 | + escape_codes["fg_%s" % name] = esc(code) |
| 89 | + escape_codes["fg_bold_%s" % name] = esc(1, code) |
| 90 | + escape_codes["fg_thin_%s" % name] = esc(2, code) |
58 | 91 |
|
59 | | -for prefix, prefix_name in PREFIXES: |
60 | | - for code, name in enumerate(COLORS): |
61 | | - escape_codes[prefix_name + name] = esc(prefix + str(code)) |
| 92 | +# Background with bg_ prefix |
| 93 | +for name, code in escape_codes_background.items(): |
| 94 | + escape_codes["bg_%s" % name] = esc(code) |
62 | 95 |
|
63 | 96 |
|
64 | | -def parse_colors(sequence: str) -> str: |
65 | | - """Return escape codes from a color sequence.""" |
66 | | - return "".join(escape_codes[n] for n in sequence.split(",") if n) |
| 97 | +def parse_colors(string: str) -> str: |
| 98 | + """Return escape codes from a color sequence string.""" |
| 99 | + return "".join(escape_codes[n] for n in string.split(",") if n) |
0 commit comments