21 lines
731 B
Python
21 lines
731 B
Python
|
|
"""在终端中渲染 Markdown(含 GFM 表格),依赖 Rich。非 TTY 时仍尽量输出为可读表格。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from loguru import logger
|
|||
|
|
from rich.console import Console
|
|||
|
|
from rich.markdown import Markdown
|
|||
|
|
|
|||
|
|
|
|||
|
|
def print_markdown_stderr(content: str) -> None:
|
|||
|
|
text = (content or "").rstrip()
|
|||
|
|
if not text:
|
|||
|
|
return
|
|||
|
|
try:
|
|||
|
|
# stderr=True 与 loguru 的默认输出一致,便于在同一终端里对齐其它日志
|
|||
|
|
console = Console(stderr=True, soft_wrap=True)
|
|||
|
|
console.print(Markdown(text))
|
|||
|
|
except Exception as exc: # pragma: no cover
|
|||
|
|
logger.warning("Rich Markdown 渲染失败 ({}), 回退为纯文本", exc)
|
|||
|
|
logger.info("{}", text)
|