Skip to content

Commit 39b83fb

Browse files
authored
Merge pull request #19 from desultory/dev
allow colorize to use color codes
2 parents 16108ab + 2d12914 commit 39b83fb

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/zenlib/util/colorize.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.1"
1+
__version__ = "0.2.0"
22

33
from enum import Enum
44

@@ -29,5 +29,13 @@ def __str__(self):
2929

3030

3131
def colorize(text: str, color="white", bright=False, bold=False) -> str:
32-
color_code = Colors[color.upper()].value
32+
try:
33+
color_code = Colors[color.upper()].value
34+
except (KeyError, AttributeError):
35+
try:
36+
color_code = int(color)
37+
except ValueError:
38+
raise ValueError(f"Invalid color: {color}")
39+
color_code = int(color)
40+
3341
return f"{ANSICode(color_code, bright, bold)}{text}{ANSICode(0)}"

tests/test_colorize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_white(self):
4848

4949

5050
def test_invalid_color(self):
51-
with self.assertRaises(KeyError):
51+
with self.assertRaises(ValueError):
5252
colorize("test", "invalid_color")
5353

5454

0 commit comments

Comments
 (0)