增加ANSI粗体等样式

This commit is contained in:
Nanaloveyuki
2025-07-28 11:51:24 +08:00
parent 9b035ca692
commit c79a7cf38b
3 changed files with 23 additions and 2 deletions

View File

@ -20,6 +20,7 @@ __all__ = [
"fmt_placeholder",
"fmt_message",
"fmt_level_name",
"set_style",
"set_color",
"set_bg_color",
"Logger" # 日志记录器非实例化

View File

@ -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",
]
"set_style"
]

View File

@ -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"