docs: update lib examples with proper context logging

This commit is contained in:
Alexander Wang 2024-12-27 16:47:41 -07:00
parent 1ea36d5d39
commit aedc7f87a5
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 14 additions and 8 deletions

View file

@ -2,7 +2,7 @@ package main
import ( import (
"context" "context"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2graph"
@ -10,6 +10,7 @@ import (
"oss.terrastruct.com/d2/d2lib" "oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog" "oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure" "oss.terrastruct.com/d2/lib/textmeasure"
"oss.terrastruct.com/util-go/go2" "oss.terrastruct.com/util-go/go2"
) )
@ -28,7 +29,8 @@ func main() {
LayoutResolver: layoutResolver, LayoutResolver: layoutResolver,
Ruler: ruler, Ruler: ruler,
} }
diagram, _, _ := d2lib.Compile(context.Background(), "x -> y", compileOpts, renderOpts) ctx := log.WithDefault(context.Background())
diagram, _, _ := d2lib.Compile(ctx, "x -> y", compileOpts, renderOpts)
out, _ := d2svg.Render(diagram, renderOpts) out, _ := d2svg.Render(diagram, renderOpts)
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600) _ = os.WriteFile(filepath.Join("out.svg"), out, 0600)
} }

View file

@ -9,6 +9,7 @@ import (
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib" "oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2oracle" "oss.terrastruct.com/d2/d2oracle"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure" "oss.terrastruct.com/d2/lib/textmeasure"
) )
@ -23,7 +24,8 @@ func main() {
LayoutResolver: layoutResolver, LayoutResolver: layoutResolver,
Ruler: ruler, Ruler: ruler,
} }
_, graph, _ := d2lib.Compile(context.Background(), "x -> y", compileOpts, nil) ctx := log.WithDefault(context.Background())
_, graph, _ := d2lib.Compile(ctx, "x -> y", compileOpts, nil)
// Create a shape with the ID, "meow" // Create a shape with the ID, "meow"
graph, _, _ = d2oracle.Create(graph, nil, "meow") graph, _, _ = d2oracle.Create(graph, nil, "meow")

View file

@ -2,7 +2,7 @@ package main
import ( import (
"context" "context"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -11,6 +11,7 @@ import (
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog" "oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure" "oss.terrastruct.com/d2/lib/textmeasure"
) )
@ -20,11 +21,12 @@ func main() {
graph.ApplyTheme(d2themescatalog.NeutralDefault.ID) graph.ApplyTheme(d2themescatalog.NeutralDefault.ID)
ruler, _ := textmeasure.NewRuler() ruler, _ := textmeasure.NewRuler()
_ = graph.SetDimensions(nil, ruler, nil) _ = graph.SetDimensions(nil, ruler, nil)
_ = d2dagrelayout.Layout(context.Background(), graph, nil) ctx := log.WithDefault(context.Background())
diagram, _ := d2exporter.Export(context.Background(), graph, nil) _ = d2dagrelayout.Layout(ctx, graph, nil)
diagram, _ := d2exporter.Export(ctx, graph, nil)
diagram.Config = config diagram.Config = config
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{ out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
ThemeID: &d2themescatalog.NeutralDefault.ID, ThemeID: &d2themescatalog.NeutralDefault.ID,
}) })
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600) _ = os.WriteFile(filepath.Join("out.svg"), out, 0600)
} }