10 // DotOpts are the options for generating a dot formatted Graph.
12 // Allows some nodes to decide to only show themselves when the user has
13 // requested the "verbose" graph.
19 // How many levels to expand modules as we draw
22 // use this to keep the cluster_ naming convention from the previous dot writer
26 // GraphNodeDotter can be implemented by a node to cause it to be included
27 // in the dot graph. The Dot method will be called which is expected to
28 // return a representation of this node.
29 type GraphNodeDotter interface {
30 // Dot is called to return the dot formatting for the node.
31 // The first parameter is the title of the node.
32 // The second parameter includes user-specified options that affect the dot
33 // graph. See GraphDotOpts below for details.
34 DotNode(string, *DotOpts) *DotNode
37 // DotNode provides a structure for Vertices to return in order to specify their
41 Attrs map[string]string
44 // Returns the DOT representation of this Graph.
45 func (g *marshalGraph) Dot(opts *DotOpts) []byte {
55 w.WriteString("digraph {\n")
59 w.WriteString(`compound = "true"` + "\n")
60 w.WriteString(`newrank = "true"` + "\n")
62 // the top level graph is written as the first subgraph
63 w.WriteString(`subgraph "root" {` + "\n")
66 // cluster isn't really used other than for naming purposes in some graphs
67 opts.cluster = opts.MaxDepth != 0
68 maxDepth := opts.MaxDepth
73 for _, s := range g.Subgraphs {
74 g.writeSubgraph(s, opts, maxDepth, &w)
82 func (v *marshalVertex) dot(g *marshalGraph, opts *DotOpts) []byte {
91 if v.graphNodeDotter != nil {
92 node := v.graphNodeDotter.DotNode(name, opts)
97 newAttrs := make(map[string]string)
98 for k, v := range attrs {
101 for k, v := range node.Attrs {
109 buf.WriteString(fmt.Sprintf(`"[%s] %s"`, graphName, name))
110 writeAttrs(&buf, attrs)
116 func (e *marshalEdge) dot(g *marshalGraph) string {
123 sourceName := g.vertexByID(e.Source).Name
124 targetName := g.vertexByID(e.Target).Name
125 s := fmt.Sprintf(`"[%s] %s" -> "[%s] %s"`, graphName, sourceName, graphName, targetName)
127 writeAttrs(&buf, e.Attrs)
132 func cycleDot(e *marshalEdge, g *marshalGraph) string {
133 return e.dot(g) + ` [color = "red", penwidth = "2.0"]`
136 // Write the subgraph body. The is recursive, and the depth argument is used to
137 // record the current depth of iteration.
138 func (g *marshalGraph) writeSubgraph(sg *marshalGraph, opts *DotOpts, depth int, w *indentWriter) {
146 // we prefix with cluster_ to match the old dot output
147 name = "cluster_" + name
148 sg.Attrs["label"] = sg.Name
150 w.WriteString(fmt.Sprintf("subgraph %q {\n", name))
151 sg.writeBody(opts, w)
153 for _, sg := range sg.Subgraphs {
154 g.writeSubgraph(sg, opts, depth, w)
158 func (g *marshalGraph) writeBody(opts *DotOpts, w *indentWriter) {
161 for _, as := range attrStrings(g.Attrs) {
162 w.WriteString(as + "\n")
165 // list of Vertices that aren't to be included in the dot output
166 skip := map[string]bool{}
168 for _, v := range g.Vertices {
169 if v.graphNodeDotter == nil {
174 w.Write(v.dot(g, opts))
177 var dotEdges []string
180 for _, c := range g.Cycles {
185 for i, j := 0, 1; i < len(c); i, j = i+1, j+1 {
192 if skip[src.ID] || skip[tgt.ID] {
197 Name: fmt.Sprintf("%s|%s", src.Name, tgt.Name),
200 Attrs: make(map[string]string),
203 dotEdges = append(dotEdges, cycleDot(e, g))
209 for _, e := range g.Edges {
210 dotEdges = append(dotEdges, e.dot(g))
213 // srot these again to match the old output
214 sort.Strings(dotEdges)
216 for _, e := range dotEdges {
217 w.WriteString(e + "\n")
224 func writeAttrs(buf *bytes.Buffer, attrs map[string]string) {
226 buf.WriteString(" [")
227 buf.WriteString(strings.Join(attrStrings(attrs), ", "))
232 func attrStrings(attrs map[string]string) []string {
233 strings := make([]string, 0, len(attrs))
234 for k, v := range attrs {
235 strings = append(strings, fmt.Sprintf("%s = %q", k, v))
237 sort.Strings(strings)
241 // Provide a bytes.Buffer like structure, which will indent when starting a
243 type indentWriter struct {
248 func (w *indentWriter) indent() {
249 newline := []byte("\n")
250 if !bytes.HasSuffix(w.Bytes(), newline) {
253 for i := 0; i < w.level; i++ {
254 w.Buffer.WriteString("\t")
258 // Indent increases indentation by 1
259 func (w *indentWriter) Indent() { w.level++ }
261 // Unindent decreases indentation by 1
262 func (w *indentWriter) Unindent() { w.level-- }
264 // the following methods intercecpt the byte.Buffer writes and insert the
265 // indentation when starting a new line.
266 func (w *indentWriter) Write(b []byte) (int, error) {
268 return w.Buffer.Write(b)
271 func (w *indentWriter) WriteString(s string) (int, error) {
273 return w.Buffer.WriteString(s)
275 func (w *indentWriter) WriteByte(b byte) error {
277 return w.Buffer.WriteByte(b)
279 func (w *indentWriter) WriteRune(r rune) (int, error) {
281 return w.Buffer.WriteRune(r)