Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/zenlib/util/colorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.1"
__version__ = "0.2.0"

from enum import Enum

Expand Down Expand Up @@ -29,5 +29,13 @@ def __str__(self):


def colorize(text: str, color="white", bright=False, bold=False) -> str:
color_code = Colors[color.upper()].value
try:
color_code = Colors[color.upper()].value
except (KeyError, AttributeError):
try:
color_code = int(color)
except ValueError:
raise ValueError(f"Invalid color: {color}")
color_code = int(color)

return f"{ANSICode(color_code, bright, bold)}{text}{ANSICode(0)}"
2 changes: 1 addition & 1 deletion tests/test_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_white(self):


def test_invalid_color(self):
with self.assertRaises(KeyError):
with self.assertRaises(ValueError):
colorize("test", "invalid_color")


Expand Down