-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
68 lines (59 loc) · 1.43 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package govisual
import (
"bytes"
"fmt"
"text/template"
)
var defaultTemplate = `digraph "{{.v.Config.Module}}" {
label="{{.v.Config.Module}}";
node [shape=Mrecord, style=solid];
{{range $key, $node := .v.Digraph.Nodes}} "{{$node.Value.FullName}}" [label="{{nodeLabel $node}}", {{if eq $node.Out 0}}shape="box",{{end}} color="{{color $node.Value.Type}}", fontcolor="{{color $node.Value.Type}}"];
{{end}}{{range $key, $edge := .v.Digraph.Edges}} "{{$edge.From.Value.FullName}}" -> "{{$edge.To.Value.FullName}}";
{{end}}
}`
func Render(v *Visualization, filenames ...string) string {
var t = template.New("dot").Funcs(map[string]interface{}{
"color": color,
"nodeLabel": nodeLabel,
})
if len(filenames) > 0 {
t = template.Must(t.ParseFiles(filenames...))
} else {
t = template.Must(t.Parse(defaultTemplate))
}
var buf bytes.Buffer
if err := t.Execute(&buf, map[string]interface{}{
"v": v,
}); err != nil {
return err.Error()
}
return buf.String()
}
func color(importType ImportType) string {
switch importType {
case Self:
return "black"
case Org:
return "orange"
case Third:
return "blue"
case Sys:
return "green"
}
return "black"
}
func nodeLabel(n *Node) string {
pkg := n.Value.(*Package)
var typ string
switch pkg.Type {
case Self:
typ = "self"
case Org:
typ = "org"
case Third:
typ = "third"
case Sys:
typ = "sys"
}
return fmt.Sprintf("%s:%s:I%d:O%d", pkg.SimpleName, typ, n.In, n.Out)
}