diff --git a/src/__init__.py b/src/__init__.py index b3ea362..abb805d 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -20,6 +20,7 @@ __all__ = [ "fmt_placeholder", "fmt_message", "fmt_level_name", + "set_style", "set_color", "set_bg_color", "Logger" # 日志记录器非实例化 diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 3ac00f7..96b1835 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -8,7 +8,7 @@ Utility functions from .configs import get_config, set_config, reset_config from .time import get_asctime, get_date, get_time, get_weekday from .fmt import fmt_console, fmt_placeholder, fmt_message, fmt_level_name -from .styles import set_color, set_bg_color +from .styles import set_color, set_bg_color, set_style __all__ = [ "get_config", @@ -24,4 +24,5 @@ __all__ = [ "fmt_level_name", "set_color", "set_bg_color", -] \ No newline at end of file + "set_style" +] diff --git a/src/utils/styles.py b/src/utils/styles.py index 2996f4c..d69c65e 100644 --- a/src/utils/styles.py +++ b/src/utils/styles.py @@ -47,3 +47,22 @@ def set_bg_color(text: str, color: str) -> str: # 将前景色ANSI代码转换为背景色代码 (38→48) ansi = ansi.replace("38;", "48;") return f"{ansi}{text}\033[0m" + +def set_style(text: str, bold: bool = False, underline: bool = False, reverse: bool = False) -> str: + """ + 设置文本样式 + Set text style + :param text: 文本内容 Text content + :param bold: 是否加粗 Is bold + :param underline: 是否下划线 Is underline + :param reverse: 是否反相 Is reverse + :return: 格式化后的文本 Formatted text + """ + ansi = "" + if bold: + ansi += "\033[1m" + if underline: + ansi += "\033[4m" + if reverse: + ansi += "\033[7m" + return f"{ansi}{text}\033[0m"