85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
"""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)
|