package main import ( "encoding/json" "fmt" "html" "os" "strings" "github.com/steveyegge/beads/internal/types" ) // renderGraphDOT renders the graph in Graphviz DOT format. // Output can be piped to graphviz: bd graph --dot | dot -Tsvg > graph.svg func renderGraphDOT(layout *GraphLayout, subgraph *TemplateSubgraph) { if len(layout.Nodes) == 0 { fmt.Println("digraph beads { }") return } fmt.Println("digraph beads {") fmt.Println(" rankdir=LR;") fmt.Println(" node [shape=box, style=\"rounded,filled\", fontname=\"Helvetica\", fontsize=11];") fmt.Println(" edge [color=\"#666666\"];") fmt.Println() // Emit nodes grouped by layer using subgraph clusters for rank alignment for layerIdx, layer := range layout.Layers { fmt.Printf(" subgraph cluster_layer_%d {\n", layerIdx) fmt.Println(" style=invis;") fmt.Printf(" rank=same;\n") for _, id := range layer { node := layout.Nodes[id] if node == nil { continue } label, fillColor, fontColor := dotNodeAttrs(node) // Escape quotes in label label = strings.ReplaceAll(label, "\"", "\\\"") fmt.Printf(" \"%s\" [label=\"%s\", fillcolor=\"%s\", fontcolor=\"%s\"];\n", dotEscapeID(id), label, fillColor, fontColor) } fmt.Println(" }") } fmt.Println() // Emit edges for _, dep := range subgraph.Dependencies { // Only include blocking dependencies in the graph if dep.Type != types.DepBlocks && dep.Type != types.DepParentChild { continue } // Ensure both endpoints exist in the subgraph if layout.Nodes[dep.IssueID] == nil || layout.Nodes[dep.DependsOnID] == nil { continue } edgeStyle := dotEdgeStyle(dep.Type) // dep.DependsOnID -> dep.IssueID (blocker points to blocked) fmt.Printf(" \"%s\" -> \"%s\"%s;\n", dotEscapeID(dep.DependsOnID), dotEscapeID(dep.IssueID), edgeStyle) } fmt.Println("}") } // dotNodeAttrs returns the DOT label, fill color, and font color for a node func dotNodeAttrs(node *GraphNode) (label, fillColor, fontColor string) { icon := statusPlainIcon(node.Issue.Status) title := truncateTitle(node.Issue.Title, 40) label = fmt.Sprintf("%s %s\\nP%d | %s", icon, node.Issue.ID, node.Issue.Priority, title) switch node.Issue.Status { case types.StatusOpen: fillColor = "#e8f4fd" fontColor = "#1a1a1a" case types.StatusInProgress: fillColor = "#fff3cd" fontColor = "#664d03" case types.StatusBlocked: fillColor = "#f8d7da" fontColor = "#842029" case types.StatusClosed: fillColor = "#d4edda" fontColor = "#888888" default: // deferred, hooked, etc. fillColor = "#e2e3e5" fontColor = "#41464b" } return } // dotEdgeStyle returns DOT edge attributes for a dependency type func dotEdgeStyle(depType types.DependencyType) string { switch depType { case types.DepBlocks: return " [style=solid, arrowhead=normal]" case types.DepParentChild: return " [style=dashed, arrowhead=empty, color=\"#999999\"]" default: return "" } } // dotEscapeID escapes an ID for DOT format by replacing characters // that could break quoted strings (backslash, double-quote). func dotEscapeID(id string) string { r := strings.NewReplacer(`\`, `\\`, `"`, `\"`) return r.Replace(id) } // statusPlainIcon returns a plain text status icon (no ANSI colors) for export formats func statusPlainIcon(status types.Status) string { switch status { case types.StatusOpen: return "○" case types.StatusInProgress: return "◐" case types.StatusBlocked: return "●" case types.StatusClosed: return "✓" default: return "❄" } } // renderGraphHTML generates a self-contained HTML file with an interactive D3.js // force-directed graph visualization. The output is a complete HTML document that // can be opened in any browser. func renderGraphHTML(layout *GraphLayout, subgraph *TemplateSubgraph) { nodes := buildHTMLGraphData(layout, subgraph) edges := buildHTMLEdgeData(layout, subgraph) nodesJSON, err := json.Marshal(nodes) if err != nil { fmt.Fprintf(os.Stderr, "Error marshaling nodes: %v\n", err) return } edgesJSON, err := json.Marshal(edges) if err != nil { fmt.Fprintf(os.Stderr, "Error marshaling edges: %v\n", err) return } title := "Beads Dependency Graph" if subgraph.Root != nil { title = fmt.Sprintf("Beads: %s (%s)", subgraph.Root.Title, subgraph.Root.ID) } if _, err := fmt.Fprintf(os.Stdout, htmlTemplate, html.EscapeString(title), string(nodesJSON), string(edgesJSON)); err != nil { fmt.Fprintf(os.Stderr, "Error writing HTML output: %v\n", err) } } // HTMLNode is the JSON structure for a node in the HTML visualization type HTMLNode struct { ID string `json:"id"` Title string `json:"title"` Status string `json:"status"` Priority int `json:"priority"` Type string `json:"type"` Layer int `json:"layer"` Assignee string `json:"assignee,omitempty"` } // HTMLEdge is the JSON structure for an edge in the HTML visualization type HTMLEdge struct { Source string `json:"source"` Target string `json:"target"` Type string `json:"type"` } func buildHTMLGraphData(layout *GraphLayout, _ *TemplateSubgraph) []HTMLNode { nodes := make([]HTMLNode, 0, len(layout.Nodes)) for _, node := range layout.Nodes { nodes = append(nodes, HTMLNode{ ID: node.Issue.ID, Title: node.Issue.Title, Status: string(node.Issue.Status), Priority: node.Issue.Priority, Type: string(node.Issue.IssueType), Layer: node.Layer, Assignee: node.Issue.Assignee, }) } return nodes } func buildHTMLEdgeData(layout *GraphLayout, subgraph *TemplateSubgraph) []HTMLEdge { edges := make([]HTMLEdge, 0, len(subgraph.Dependencies)) for _, dep := range subgraph.Dependencies { if dep.Type != types.DepBlocks && dep.Type != types.DepParentChild { continue } if layout.Nodes[dep.IssueID] == nil || layout.Nodes[dep.DependsOnID] == nil { continue } edges = append(edges, HTMLEdge{ Source: dep.DependsOnID, Target: dep.IssueID, Type: string(dep.Type), }) } return edges } // htmlTemplate is the self-contained HTML template with embedded D3.js visualization. // Uses CDN-hosted D3.js v7 for the force-directed graph. const htmlTemplate = ` %s

Status

Open
In Progress
Blocked
Closed
Deferred

Edges

blocks
parent-child
Drag nodes to rearrange. Scroll to zoom. Click for details.
`