Skip to content

Commit

Permalink
default layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Dec 30, 2022
1 parent 97078e5 commit 115d6e4
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/d2plugin-dagre/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

func main() {
xmain.Main(d2plugin.Serve(d2plugin.DagrePlugin))
xmain.Main(d2plugin.Serve(&d2plugin.DagrePlugin))
}
2 changes: 1 addition & 1 deletion d2chaos/d2chaos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func test(t *testing.T, textPath, text string) {
err = g.SetDimensions(nil, ruler, nil)
assert.Nil(t, err)

err = d2dagrelayout.Layout(ctx, g)
err = d2dagrelayout.DefaultLayout(ctx, g)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion d2exporter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func run(t *testing.T, tc testCase) {
err = g.SetDimensions(nil, ruler, nil)
assert.JSON(t, nil, err)

err = d2sequence.Layout(ctx, g, d2dagrelayout.Layout)
err = d2sequence.Layout(ctx, g, d2dagrelayout.DefaultLayout)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 4 additions & 0 deletions d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type dagreOpts struct {
ConfigurableOpts
}

func DefaultLayout(ctx context.Context, g *d2graph.Graph) (err error) {
return Layout(ctx, g, nil)
}

func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err error) {
if opts == nil {
opts = &DefaultOpts
Expand Down
4 changes: 4 additions & 0 deletions d2layouts/d2elklayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ type elkOpts struct {
ConfigurableOpts
}

func DefaultLayout(ctx context.Context, g *d2graph.Graph) (err error) {
return Layout(ctx, g, nil)
}

func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err error) {
if opts == nil {
opts = &DefaultOpts
Expand Down
2 changes: 1 addition & 1 deletion d2renderers/d2sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func run(t *testing.T, tc testCase) {
diagram, _, err := d2lib.Compile(ctx, tc.script, &d2lib.CompileOptions{
Ruler: ruler,
ThemeID: 0,
Layout: d2dagrelayout.Layout,
Layout: d2dagrelayout.DefaultLayout,
FontFamily: go2.Pointer(d2fonts.HandDrawn),
})
if !tassert.Nil(t, err) {
Expand Down
2 changes: 1 addition & 1 deletion d2renderers/d2svg/appendix/appendix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func run(t *testing.T, tc testCase) {
diagram, _, err := d2lib.Compile(ctx, tc.script, &d2lib.CompileOptions{
Ruler: ruler,
ThemeID: 0,
Layout: d2dagrelayout.Layout,
Layout: d2dagrelayout.DefaultLayout,
})
if !tassert.Nil(t, err) {
return
Expand Down
6 changes: 5 additions & 1 deletion docs/examples/lib/1-d2lib/d2lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"path/filepath"

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg"
Expand All @@ -15,8 +16,11 @@ import (
// Remember to add if err != nil checks in production.
func main() {
ruler, _ := textmeasure.NewRuler()
defaultLayout := func(ctx context.Context, g *d2graph.Graph) error {
return d2dagrelayout.Layout(ctx, g, nil)
}
diagram, _, _ := d2lib.Compile(context.Background(), "x -> y", &d2lib.CompileOptions{
Layout: d2dagrelayout.Layout,
Layout: defaultLayout,
Ruler: ruler,
ThemeID: d2themescatalog.GrapeSoda.ID,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/lib/2-d2oracle/d2oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
// From one.go
ruler, _ := textmeasure.NewRuler()
_, graph, _ := d2lib.Compile(context.Background(), "x -> y", &d2lib.CompileOptions{
Layout: d2dagrelayout.Layout,
Layout: d2dagrelayout.DefaultLayout,
Ruler: ruler,
ThemeID: d2themescatalog.GrapeSoda.ID,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/lib/3-lowlevel/lowlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
graph, _ := d2compiler.Compile("", strings.NewReader("x -> y"), nil)
ruler, _ := textmeasure.NewRuler()
_ = graph.SetDimensions(nil, ruler, nil)
_ = d2dagrelayout.Layout(context.Background(), graph)
_ = d2dagrelayout.Layout(context.Background(), graph, nil)
diagram, _ := d2exporter.Export(context.Background(), graph, d2themescatalog.NeutralDefault.ID, nil)
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
Pad: d2svg.DEFAULT_PADDING,
Expand Down
4 changes: 2 additions & 2 deletions e2etests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func run(t *testing.T, tc testCase) {
for _, layoutName := range layoutsTested {
var layout func(context.Context, *d2graph.Graph) error
if layoutName == "dagre" {
layout = d2dagrelayout.Layout
layout = d2dagrelayout.DefaultLayout
} else if layoutName == "elk" {
layout = d2elklayout.Layout
layout = d2elklayout.DefaultLayout
}
diagram, _, err := d2lib.Compile(ctx, tc.script, &d2lib.CompileOptions{
Ruler: ruler,
Expand Down

0 comments on commit 115d6e4

Please sign in to comment.