🐛 修复了颜色插值为空无法渲染的问题

This commit is contained in:
Nanaloveyuki
2025-07-28 17:52:05 +08:00
parent b83984d58a
commit 5a7756b793
5 changed files with 26 additions and 11 deletions

View File

@ -28,5 +28,6 @@
"WARN": "#fff600",
"ERRO": "#ffa000",
"CRIT": "#ff8181"
}
},
"time_color": "#28ffb6"
}

View File

@ -33,6 +33,7 @@ DEFAULT_CONFIG = {
"weekday_format": "%A",
"level_name": {"DEBUG": "DEBUG", "INFO": "INFO", "WARN": "WARN", "ERRO": "ERRO", "CRIT": "CRIT"},
"level_color": {"DEBUG": "#c1d5ff", "INFO": "#c1ffff", "WARN": "#fff600", "ERRO": "#ffa000", "CRIT": "#ff8181"},
"time_color": "#28ffb6",
}
g_config_cache = None

View File

@ -12,6 +12,8 @@ from .time import get_asctime, get_time, get_weekday, get_date
from .styles import set_color, set_bg_color
import re
time_color = get_config("time_color")
def fmt_level(level: str) -> int:
"""
格式化日志级别
@ -64,10 +66,10 @@ def fmt_placeholder(message: Any, use_date_color: bool = True) -> str:
message = str(message)
if use_date_color:
message = message.format_map(SafeDict(
asctime = set_color(get_asctime(),"#28ffb6"),
time = set_color(get_time(),"#28ffb6"),
weekday = set_color(get_weekday(),"#28ffb6"),
date = set_color(get_date(),"#28ffb6")
asctime = set_color(get_asctime(),time_color),
time = set_color(get_time(),time_color),
weekday = set_color(get_weekday(),time_color),
date = set_color(get_date(),time_color)
))
else:
message = message.format_map(SafeDict(

View File

@ -8,19 +8,21 @@ py-logiliteal's style tools, used to format log output
from typing import Union, Optional
def _get_hex_to_ansi(hex_color: str) -> Union[Optional[str], None]:
def _get_hex_to_ansi(hex_color: str|None) -> Union[Optional[str], None]:
"""
将16进制颜色值转换为ANSI转义序列
Convert hex color value to ANSI escape sequence
:param hex_color: 16进制颜色值 Hex color value
:return: ANSI转义序列 ANSI escape sequence
"""
if not hex_color:
return ""
if not hex_color.startswith("#"):
return None
r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
return f"\033[38;2;{r};{g};{b}m"
def set_color(text: str, color: str) -> str:
def set_color(text: str|None, color: str|None) -> str:
"""
设置文本颜色
Set text color
@ -28,12 +30,16 @@ def set_color(text: str, color: str) -> str:
:param color: 颜色值 Color value
:return: 格式化后的文本 Formatted text
"""
if not text:
return ""
if not color:
return text
ansi = _get_hex_to_ansi(color)
if not ansi:
return text
return f"{ansi}{text}\033[0m"
def set_bg_color(text: str, color: str) -> str:
def set_bg_color(text: str|None, color: str|None) -> str:
"""
设置文本背景颜色
Set text background color
@ -41,14 +47,17 @@ def set_bg_color(text: str, color: str) -> str:
:param color: 颜色值 Color value
:return: 格式化后的文本 Formatted text
"""
if not text:
return ""
if not color:
return text
ansi = _get_hex_to_ansi(color)
if not ansi:
return text
# 将前景色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:
def set_style(text: str|None, bold: bool = False, underline: bool = False, reverse: bool = False) -> str:
"""
设置文本样式
Set text style
@ -58,6 +67,8 @@ def set_style(text: str, bold: bool = False, underline: bool = False, reverse: b
:param reverse: 是否反相 Is reverse
:return: 格式化后的文本 Formatted text
"""
if not text:
return ""
ansi = ""
if bold:
ansi += "\033[1m"

View File

@ -4,7 +4,7 @@ from pathlib import Path
project_root = Path(__file__).parent.parent
sys.path.append(str(project_root))
from src.logiliteal.utils.configs import get_config, set_config, reset_config
from src.logiliteal.utils.configs import get_config, set_config, reset_config, create_backup
print(f"\n配置更换测试")
set_config("file_level", "DEBUG")