From d11fcab5970034f922ceb8de2a7d8bb27de4b449 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 30 Jan 2026 05:30:56 +0000 Subject: [PATCH] Add exporters, utils, and tests --- .termflow/exporters/ascii_generator.py | 84 ++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 .termflow/exporters/ascii_generator.py diff --git a/.termflow/exporters/ascii_generator.py b/.termflow/exporters/ascii_generator.py new file mode 100644 index 0000000..7416dc6 --- /dev/null +++ b/.termflow/exporters/ascii_generator.py @@ -0,0 +1,84 @@ +"""ASCII diagram generation.""" + +from dataclasses import dataclass +from typing import List, Optional + + +@dataclass +class FlowNode: + """Represents a node in a flowchart.""" + id: str + label: str + node_type: str = "process" + + +class ASCIIGenerator: + """Generates ASCII flowcharts.""" + + def __init__(self, style: str = "detailed"): + self.style = style + self.node_width = 40 + + def generate_from_commands( + self, + commands: List[str], + metadata: Optional[dict] = None, + ) -> str: + """Generate ASCII diagram from commands.""" + lines = [] + + if self.style == "detailed": + lines.append("=" * 60) + title = metadata.get("title", "Command Flow") if metadata else "Command Flow" + lines.append(f" {title}") + lines.append("=" * 60) + lines.append("") + + node_id = 0 + for i, cmd in enumerate(commands): + if self.style == "minimal": + lines.append(f"{i + 1}. {cmd}") + else: + lines.append(self._create_node(node_id, cmd)) + node_id += 1 + + if i < len(commands) - 1: + if self.style == "detailed": + lines.append(" |") + lines.append(" v") + else: + lines.append(" |") + lines.append(" v") + + if metadata and self.style == "detailed": + lines.append("") + lines.append("-" * 60) + lines.append(f"Total Commands: {metadata.get('command_count', len(commands))}") + + return "\n".join(lines) + + def _create_node(self, node_id: int, command: str) -> str: + """Create a single node in the flowchart.""" + label = command[: self.node_width - 4] + "..." if len(command) > self.node_width - 4 else command + return f"[ {label} ]" + + +class GitGraphGenerator: + """Generates ASCII git graphs.""" + + def __init__(self): + self.max_hash_length = 8 + + def generate_from_parser(self, parser) -> str: + """Generate ASCII git graph from parser.""" + lines = [] + + for commit in parser.commits: + hash_str = commit.hash[: self.max_hash_length] + + if commit.parents: + lines.append(f"* {hash_str} - {commit.message}") + else: + lines.append(f"* {hash_str} - {commit.message}") + + return "\n".join(lines)