forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_writer.go
47 lines (40 loc) · 853 Bytes
/
graph_writer.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
package dot
import (
"bytes"
"fmt"
)
// graphWriter wraps a bytes.Buffer and tracks indent level levels.
type graphWriter struct {
bytes.Buffer
indent int
indentStr string
}
// Returns an initialized graphWriter at indent level 0.
func newGraphWriter() *graphWriter {
w := &graphWriter{
indent: 0,
}
w.init()
return w
}
// Prints to the buffer at the current indent level.
func (w *graphWriter) Printf(s string, args ...interface{}) {
w.WriteString(w.indentStr + fmt.Sprintf(s, args...))
}
// Increase the indent level.
func (w *graphWriter) Indent() {
w.indent++
w.init()
}
// Decrease the indent level.
func (w *graphWriter) Unindent() {
w.indent--
w.init()
}
func (w *graphWriter) init() {
indentBuf := new(bytes.Buffer)
for i := 0; i < w.indent; i++ {
indentBuf.WriteString("\t")
}
w.indentStr = indentBuf.String()
}