Add exporters, utils, and tests
This commit is contained in:
46
.termflow/exporters/mermaid_generator.py
Normal file
46
.termflow/exporters/mermaid_generator.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
"""Mermaid diagram generation."""
|
||||||
|
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class MermaidGenerator:
|
||||||
|
"""Generates Mermaid diagrams."""
|
||||||
|
|
||||||
|
def generate_flowchart(
|
||||||
|
self,
|
||||||
|
commands: List[str],
|
||||||
|
title: str = "Command Flow",
|
||||||
|
metadata: Optional[dict] = None,
|
||||||
|
) -> str:
|
||||||
|
"""Generate Mermaid flowchart from commands."""
|
||||||
|
lines = [f"flowchart TD"]
|
||||||
|
lines.append(f" title({title})")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
for i, cmd in enumerate(commands):
|
||||||
|
safe_label = cmd.replace('"', "'").replace("(", "").replace(")", "")
|
||||||
|
lines.append(f' n{i}[\"{safe_label}\"]')
|
||||||
|
|
||||||
|
if i > 0:
|
||||||
|
lines.append(f" n{i-1} --> n{i}")
|
||||||
|
|
||||||
|
if metadata:
|
||||||
|
lines.append("")
|
||||||
|
lines.append(f" subgraph Stats")
|
||||||
|
lines.append(f' command_count(\`Commands: {metadata.get("command_count", len(commands))}\`)')
|
||||||
|
lines.append(" end")
|
||||||
|
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
def generate_git_graph_from_parser(self, parser) -> str:
|
||||||
|
"""Generate Mermaid git graph from parser."""
|
||||||
|
lines = ["gitGraph"]
|
||||||
|
lines.append(" commit")
|
||||||
|
|
||||||
|
for commit in parser.commits[1:]:
|
||||||
|
if len(commit.parents) > 1:
|
||||||
|
lines.append(" merge")
|
||||||
|
else:
|
||||||
|
lines.append(" commit")
|
||||||
|
|
||||||
|
return "\n".join(lines)
|
||||||
Reference in New Issue
Block a user