github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/dot/graph_writer.go (about)

     1  package dot
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  // graphWriter wraps a bytes.Buffer and tracks indent level levels.
     9  type graphWriter struct {
    10  	bytes.Buffer
    11  	indent    int
    12  	indentStr string
    13  }
    14  
    15  // Returns an initialized graphWriter at indent level 0.
    16  func newGraphWriter() *graphWriter {
    17  	w := &graphWriter{
    18  		indent: 0,
    19  	}
    20  	w.init()
    21  	return w
    22  }
    23  
    24  // Prints to the buffer at the current indent level.
    25  func (w *graphWriter) Printf(s string, args ...interface{}) {
    26  	w.WriteString(w.indentStr + fmt.Sprintf(s, args...))
    27  }
    28  
    29  // Increase the indent level.
    30  func (w *graphWriter) Indent() {
    31  	w.indent++
    32  	w.init()
    33  }
    34  
    35  // Decrease the indent level.
    36  func (w *graphWriter) Unindent() {
    37  	w.indent--
    38  	w.init()
    39  }
    40  
    41  func (w *graphWriter) init() {
    42  	indentBuf := new(bytes.Buffer)
    43  	for i := 0; i < w.indent; i++ {
    44  		indentBuf.WriteString("\t")
    45  	}
    46  	w.indentStr = indentBuf.String()
    47  }