Skip to content

Commit 7d17097

Browse files
committed
Support 'light' colour codes
This fixes #87
1 parent 58f9204 commit 7d17097

2 files changed

Lines changed: 72 additions & 39 deletions

File tree

colorlog/escape_codes.py

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,81 @@
1919

2020

2121
# 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"
2424

2525

26-
# The initial list of escape codes
2726
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),
3130
}
3231

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)
4485

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)
5891

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)
6295

6396

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)

colorlog/tests/test_escape_codes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def test_reset():
1414

1515

1616
def test_bold_color():
17-
assert escape_codes["bold_red"] == "\033[01;31m"
17+
assert escape_codes["bold_red"] == "\033[1;31m"
1818

1919

2020
def test_fg_color():
21-
assert escape_codes["fg_bold_yellow"] == "\033[01;33m"
21+
assert escape_codes["fg_bold_yellow"] == "\033[1;33m"
2222

2323

2424
def test_bg_color():
@@ -44,7 +44,7 @@ def test_parse_colors():
4444

4545

4646
def test_parse_multiple_colors():
47-
assert parse_colors("bold_red,bg_bold_blue") == "\033[01;31m\033[104m"
47+
assert parse_colors("bold_red,bg_bold_blue") == "\033[1;31m\033[104m"
4848

4949

5050
def test_parse_invalid_colors():

0 commit comments

Comments
 (0)