Install
openclaw skills install termaidRender Mermaid diagrams as colored ASCII/Unicode text directly in terminals or Python apps without external dependencies.
openclaw skills install termaidTermaid 是一个纯 Python 的 Mermaid 图表渲染工具,可以在终端或 Python 应用中直接渲染 Mermaid 图表为 ASCII/Unicode 字符图形。
# 基本安装
pip install termaid
# 带彩色输出
pip install termaid[rich]
# 带 TUI 界面
pip install termaid[textual]
# 试用(无需安装)
uvx termaid diagram.mmd
# 渲染文件
termaid diagram.mmd
# 管道输入
echo "graph LR; A-->B-->C" | termaid
# 使用主题
termaid diagram.mmd --theme neon
# ASCII 模式
termaid diagram.mmd --ascii
# TUI 交互界面
termaid diagram.mmd --tui
from termaid import render
# 基础渲染
print(render("graph LR\n A --> B --> C"))
# 彩色渲染
from termaid import render_rich
from rich import print as rprint
rprint(render_rich("graph LR\n A --> B", theme="terra"))
# Textual TUI 组件
from termaid import MermaidWidget
widget = MermaidWidget("graph LR\n A --> B")
graph TD
A[Start] --> B{Is valid?}
B -->|Yes| C(Process)
C --> D([Done])
B -->|No| E[Error]
特性:
sequenceDiagram
Alice->>Bob: Hello Bob
Bob-->>Alice: Hi Alice
Alice->>Bob: How are you?
Bob-->>Alice: Great!
classDiagram
class Animal {
+String name
+int age
+makeSound()
}
Animal <|-- Dog
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
stateDiagram-v2
[*] --> Idle
Idle --> Processing : start
Processing --> Done : complete
Done --> [*]
block-beta
columns 3
A["Frontend"] B["API"] C["Database"]
gitGraph
commit id: "init"
commit id: "feat"
branch develop
commit id: "dev-1"
checkout main
merge develop id: "merge"
pie title Pets adopted by volunteers
"Dogs" : 386
"Cats" : 85
"Rats" : 15
注意:饼图以水平条形图形式渲染,更易于终端阅读
treemap-beta
"Frontend"
"React": 40
"CSS": 15
"Backend"
"API": 35
"Auth": 10
termaid diagram.mmd --theme neon
termaid diagram.mmd --theme phosphor
rprint(render_rich(source, theme="amber"))
| 选项 | 描述 |
|---|---|
--tui | 交互式 TUI 查看器 |
--ascii | ASCII 纯文本输出(无 Unicode) |
--theme NAME | 颜色主题:default, terra, neon, mono, amber, phosphor |
--padding-x N | 框内水平填充(默认:4) |
--padding-y N | 框内垂直填充(默认:2) |
--sharp-edges | 边角使用锐角而非圆角 |
# 在 Markdown 文档中嵌入 Mermaid 图表的终端版本
termaid architecture.mmd >> README.md
# 在 CI 日志中显示架构图
echo "graph LR; Frontend-->API-->Database" | termaid --ascii
# 在代码中动态生成图表
from termaid import render
print(render(class_diagram_source))
# 在 Textual 应用中显示动态图表
from termaid import MermaidWidget
from textual.app import App
class DiagramApp(App):
def compose(self):
yield MermaidWidget("graph LR\n A --> B")
# 在远程服务器上查看图表
ssh user@server "cat diagram.mmd | termaid"
#!/bin/bash
# 自动生成架构文档
# 1. 从代码生成 Mermaid 图
python generate_architecture_mmd.py > arch.mmd
# 2. 渲染为终端友好的格式
termaid arch.mmd --theme neon > terminal_arch.txt
# 3. 嵌入到文档中
cat terminal_arch.txt >> ARCHITECTURE.md
# 在 AI Agent 中使用 Termaid 可视化架构
def visualize_architecture(agent_response):
"""
将 AI 生成的架构描述转换为可视化图表
"""
mermaid_source = agent_response.get('mermaid', '')
if mermaid_source:
from termaid import render
return render(mermaid_source)
return "No diagram to render"
#!/bin/bash
# 批量转换 Mermaid 文件
for file in *.mmd; do
output="${file%.mmd}.txt"
echo "Converting $file to $output"
termaid "$file" --theme terra > "$output"
done
#!/bin/bash
# 监控架构变化并重新渲染
WATCH_DIR="./diagrams"
RENDER_DIR="./rendered"
inotifywait -m -e modify "$WATCH_DIR" |
while read -r directory events filename; do
if [[ "$filename" == *.mmd ]]; then
echo "Rendering $filename..."
termaid "$WATCH_DIR/$filename" \
--theme phosphor \
> "$RENDER_DIR/${filename%.mmd}.txt"
fi
done
from termaid import render
# 带自定义参数
result = render(
mermaid_source,
padding_x=8,
padding_y=4,
sharp_edges=True
)
from termaid import render
import sys
try:
output = render(invalid_mermaid_source)
except Exception as e:
print(f"渲染失败: {e}", file=sys.stderr)
# 提供降级方案
print("graph TD\n Error[渲染错误] --> Help[检查语法]")
# 预加载常用图表
from termaid import render
import functools
@functools.lru_cache(maxsize=100)
def cached_render(source: str) -> str:
"""缓存渲染结果"""
return render(source)
编码问题
# 确保终端支持 UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
颜色不显示
# 安装 rich 扩展
pip install termaid[rich]
# 或使用 ASCII 模式
termaid diagram.mmd --ascii
图表过大
# 调整填充
termaid diagram.mmd --padding-x 2 --padding-y 1
渲染失败
# 检查 Mermaid 语法
# 确保使用支持的图表类型
import termaid
import logging
# 启用调试日志
logging.basicConfig(level=logging.DEBUG)
# 或查看内部状态
diagram = termaid.parse_mermaid(source)
print(f"图表类型: {diagram.type}")
print(f"节点数: {len(diagram.nodes)}")
# 创建自定义颜色主题
custom_theme = {
"node_color": "#FF5733",
"edge_color": "#33FF57",
"label_color": "#FFFFFF"
}
# 导出为其他格式
def export_as_html(mermaid_source, termaid_output):
html = f"""
<div class="terminal-diagram">
<pre>{termaid_output}</pre>
<details>
<summary>Mermaid 源代码</summary>
<pre>{mermaid_source}</pre>
</details>
</div>
"""
return html
# 与 Graphviz 结合
termaid diagram.mmd | dot -Tpng > diagram.png
许可证: MIT
Python 要求: >= 3.11
作者: Fabio Souto (@fasouto)
项目状态: Alpha(积极开发中)