From c79a7cf38b73eaca778b7cf6274ee8ee4f3457d2 Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Mon, 28 Jul 2025 11:51:24 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A2=9E=E5=8A=A0ANSI=E7=B2=97?= =?UTF-8?q?=E4=BD=93=E7=AD=89=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__init__.py | 1 + src/utils/__init__.py | 5 +++-- src/utils/styles.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) 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"