-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
visualize.go
66 lines (58 loc) · 1.25 KB
/
visualize.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
package endure
import (
"bytes"
"os"
"strconv"
"strings"
"text/template"
"github.com/roadrunner-server/endure/v2/graph"
"github.com/roadrunner-server/errors"
)
var _graphTmpl = template.Must(
template.New("DotGraph").
Funcs(template.FuncMap{
"quote": strconv.Quote,
}).
Parse(`digraph endure {
rankdir=TB;
graph [compound=true];
{{range $v := .}}
{{range $g := $v.Dependencies}}
{{quote $v.ID}} -> {{quote $g.ID}};
{{end}}
{{end}}
}`))
// Visualize visualizes the graph based on provided output value
func (e *Endure) Visualize(vertices []*graph.Vertex) error {
const op = errors.Op("endure_visualize")
f := new(bytes.Buffer)
err := _graphTmpl.Execute(f, vertices)
if err != nil {
return errors.E(op, err)
}
// clear on exit
defer f.Reset()
// clean up
strSl := strings.Split(f.String(), "\n")
// remove old data from buffer
f.Reset()
// set last string, because vertices are not distinct
var last string
for i := 0; i < len(strSl); i++ {
if strSl[i] == last {
// skip
continue
}
if strSl[i] == "\t" || strSl[i] == "\t\t" {
// skip tabs
continue
}
last = strSl[i]
f.WriteString(strSl[i] + "\n")
}
_, err = os.Stdout.WriteString(f.String())
if err != nil {
return errors.E(op, err)
}
return nil
}