-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathplugin_dagre.go
98 lines (83 loc) · 2.12 KB
/
plugin_dagre.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//go:build !nodagre
package d2plugin
import (
"context"
"encoding/json"
"fmt"
"sync"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/util-go/xmain"
)
var DagrePlugin = dagrePlugin{}
func init() {
plugins = append(plugins, &DagrePlugin)
}
type dagrePlugin struct {
mu sync.Mutex
opts *d2dagrelayout.ConfigurableOpts
}
func (p *dagrePlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
return []PluginSpecificFlag{
{
Name: "dagre-nodesep",
Type: "int64",
Default: int64(d2dagrelayout.DefaultOpts.NodeSep),
Usage: "number of pixels that separate nodes horizontally.",
Tag: "nodesep",
},
{
Name: "dagre-edgesep",
Type: "int64",
Default: int64(d2dagrelayout.DefaultOpts.EdgeSep),
Usage: "number of pixels that separate edges horizontally.",
Tag: "edgesep",
},
}, nil
}
func (p *dagrePlugin) HydrateOpts(opts []byte) error {
p.mu.Lock()
defer p.mu.Unlock()
if opts != nil {
var dagreOpts d2dagrelayout.ConfigurableOpts
err := json.Unmarshal(opts, &dagreOpts)
if err != nil {
return xmain.UsageErrorf("non-dagre layout options given for dagre")
}
p.opts = &dagreOpts
}
return nil
}
func (p *dagrePlugin) Info(ctx context.Context) (*PluginInfo, error) {
p.mu.Lock()
defer p.mu.Unlock()
opts := xmain.NewOpts(nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
return nil, err
}
for _, f := range flags {
f.AddToOpts(opts)
}
return &PluginInfo{
Name: "dagre",
Type: "bundled",
Features: []PluginFeature{},
ShortHelp: "The directed graph layout library Dagre",
LongHelp: fmt.Sprintf(`dagre is a directed graph layout library for JavaScript.
See https://d2lang.com/tour/dagre for more.
Flags correspond to ones found at https://github.com/dagrejs/dagre/wiki.
Flags:
%s
`, opts.Defaults()),
}, nil
}
func (p *dagrePlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
p.mu.Lock()
optsCopy := *p.opts
p.mu.Unlock()
return d2dagrelayout.Layout(ctx, g, &optsCopy)
}
func (p *dagrePlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
return in, nil
}