-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.go
48 lines (43 loc) · 906 Bytes
/
render.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
package leetcode
import (
"bytes"
"fmt"
"github.com/goccy/go-graphviz"
"github.com/goccy/go-graphviz/cgraph"
"os"
"os/exec"
"runtime"
)
func render(f func(graph *cgraph.Graph)) {
g := graphviz.New()
graph, err := g.Graph()
if err != nil {
panic(err)
}
f(graph)
var buf bytes.Buffer
if err := g.Render(graph, graphviz.PNG, &buf); err != nil {
panic(err)
}
path := os.TempDir() + `/data.png`
if err := g.RenderFilename(graph, graphviz.PNG, path); err != nil {
panic(err)
}
openbrowser(`file://` + path)
}
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
panic(err)
}
}