diff --git a/cmd/digraph/digraph.go b/cmd/digraph/digraph.go
index 5253ae8..8a7a462 100644
--- a/cmd/digraph/digraph.go
+++ b/cmd/digraph/digraph.go
@@ -29,6 +29,7 @@
"iter"
"golang.org/x/tools/internal/graph"
+ "golang.org/x/tools/internal/graph/graphfmt"
)
func usage() {
@@ -167,20 +168,6 @@
return nil
}
-func (g digraph) toDot(w *bytes.Buffer) {
- fmt.Fprintln(w, "digraph {")
- for _, src := range g.nodelist() {
- for _, dst := range g[src].sort() {
- // Dot's quoting rules appear to align with Go's for escString,
- // which is the syntax of node IDs. Labels require significantly
- // more quoting, but that appears not to be necessary if the node ID
- // is implicitly used as the label.
- fmt.Fprintf(w, "\t%q -> %q;\n", src, dst)
- }
- }
- fmt.Fprintln(w, "}")
-}
-
func parse(rd io.Reader) (digraph, error) {
g := make(digraph)
@@ -394,9 +381,7 @@
if len(args) != 1 || args[0] != "dot" {
return fmt.Errorf("usage: digraph to dot")
}
- var b bytes.Buffer
- g.toDot(&b)
- stdout.Write(b.Bytes())
+ graphfmt.Dot[string]{}.Fprint(stdout, g)
default:
return fmt.Errorf("no such command %q", cmd)
diff --git a/cmd/digraph/digraph_test.go b/cmd/digraph/digraph_test.go
index 74e61ee..153b253 100644
--- a/cmd/digraph/digraph_test.go
+++ b/cmd/digraph/digraph_test.go
@@ -387,11 +387,15 @@
in := `a b c
b "d\"\\d"
c "d\"\\d"`
- want := `digraph {
- "a" -> "b";
- "a" -> "c";
- "b" -> "d\"\\d";
- "c" -> "d\"\\d";
+ want := `digraph "" {
+n0 [label="a"];
+n1 [label="b"];
+n2 [label="c"];
+n3 [label="d\"\\d"];
+n0 -> n1;
+n0 -> n2;
+n1 -> n3;
+n2 -> n3;
}
`
defer func(in io.Reader, out io.Writer) { stdin, stdout = in, out }(stdin, stdout)