增强 ModuleColoredConsoleRenderer,支持 Rich 标记语言解析,改进消息内容着色逻辑
This commit is contained in:
@@ -861,7 +861,7 @@ class ModuleColoredConsoleRenderer:
|
|||||||
else:
|
else:
|
||||||
parts.append(Text(module_text))
|
parts.append(Text(module_text))
|
||||||
|
|
||||||
# 消息内容(确保转换为字符串)
|
# 消息内容(确保转换为字符串)并支持 Rich 标记
|
||||||
event_content = ""
|
event_content = ""
|
||||||
if isinstance(event, str):
|
if isinstance(event, str):
|
||||||
event_content = event
|
event_content = event
|
||||||
@@ -875,10 +875,10 @@ class ModuleColoredConsoleRenderer:
|
|||||||
# 其他类型直接转换为字符串
|
# 其他类型直接转换为字符串
|
||||||
event_content = str(event)
|
event_content = str(event)
|
||||||
|
|
||||||
# 在 full 模式下为消息内容着色
|
# 在 full 模式下为消息内容着色,并支持 Rich 标记语言
|
||||||
if self._colors and self._enable_full_content_colors:
|
if self._colors and self._enable_full_content_colors:
|
||||||
if "内心思考:" in event_content:
|
if "内心思考:" in event_content:
|
||||||
# 使用明亮的粉色用于“内心思考”段落
|
# 使用明亮的粉色用于"内心思考"段落
|
||||||
thought_hex_color = "#FFAFD7"
|
thought_hex_color = "#FFAFD7"
|
||||||
prefix, thought = event_content.split("内心思考:", 1)
|
prefix, thought = event_content.split("内心思考:", 1)
|
||||||
|
|
||||||
@@ -888,28 +888,43 @@ class ModuleColoredConsoleRenderer:
|
|||||||
# 组合为一个 Text,避免 join 时插入多余空格
|
# 组合为一个 Text,避免 join 时插入多余空格
|
||||||
content_text = Text()
|
content_text = Text()
|
||||||
if prefix:
|
if prefix:
|
||||||
|
# 解析 prefix 中的 Rich 标记
|
||||||
if module_hex_color:
|
if module_hex_color:
|
||||||
content_text.append(prefix, style=module_hex_color)
|
content_text.append(Text.from_markup(prefix, style=module_hex_color))
|
||||||
else:
|
else:
|
||||||
content_text.append(prefix)
|
content_text.append(Text.from_markup(prefix))
|
||||||
|
|
||||||
# 与“内心思考”段落之间插入空行
|
# 与"内心思考"段落之间插入空行
|
||||||
if prefix:
|
if prefix:
|
||||||
content_text.append("\n\n")
|
content_text.append("\n\n")
|
||||||
|
|
||||||
# “内心思考”标题+内容
|
# "内心思考"标题+内容
|
||||||
content_text.append("内心思考:", style=thought_hex_color)
|
content_text.append("内心思考:", style=thought_hex_color)
|
||||||
if thought:
|
if thought:
|
||||||
content_text.append(thought, style=thought_hex_color)
|
content_text.append(thought, style=thought_hex_color)
|
||||||
|
|
||||||
parts.append(content_text)
|
parts.append(content_text)
|
||||||
else:
|
else:
|
||||||
|
# 使用 Text.from_markup 解析 Rich 标记语言
|
||||||
if module_hex_color:
|
if module_hex_color:
|
||||||
parts.append(Text(event_content, style=module_hex_color))
|
try:
|
||||||
|
parts.append(Text.from_markup(event_content, style=module_hex_color))
|
||||||
|
except Exception:
|
||||||
|
# 如果标记解析失败,回退到普通文本
|
||||||
|
parts.append(Text(event_content, style=module_hex_color))
|
||||||
else:
|
else:
|
||||||
parts.append(Text(event_content))
|
try:
|
||||||
|
parts.append(Text.from_markup(event_content))
|
||||||
|
except Exception:
|
||||||
|
# 如果标记解析失败,回退到普通文本
|
||||||
|
parts.append(Text(event_content))
|
||||||
else:
|
else:
|
||||||
parts.append(Text(event_content))
|
# 即使在非 full 模式下,也尝试解析 Rich 标记(但不应用颜色)
|
||||||
|
try:
|
||||||
|
parts.append(Text.from_markup(event_content))
|
||||||
|
except Exception:
|
||||||
|
# 如果标记解析失败,使用普通文本
|
||||||
|
parts.append(Text(event_content))
|
||||||
|
|
||||||
# 处理其他字段
|
# 处理其他字段
|
||||||
extras = []
|
extras = []
|
||||||
|
|||||||
Reference in New Issue
Block a user