hcl/printer/printer.go

42 lines
765 B
Go
Raw Normal View History

package printer
2015-10-03 00:30:57 +00:00
import (
"io"
"text/tabwriter"
"github.com/fatih/hcl/ast"
2015-10-03 00:30:57 +00:00
)
type printer struct {
cfg Config
2015-10-03 00:30:57 +00:00
}
// A Config node controls the output of Fprint.
type Config struct {
SpacesWidth int // if set, it will use spaces instead of tabs for alignment
2015-10-03 00:30:57 +00:00
}
func (c *Config) Fprint(output io.Writer, node ast.Node) error {
2015-10-03 00:30:57 +00:00
p := &printer{
cfg: *c,
2015-10-03 00:30:57 +00:00
}
if _, err := output.Write(p.output(node)); err != nil {
2015-10-03 00:30:57 +00:00
return err
}
// flush tabwriter, if any
var err error
if tw, _ := output.(*tabwriter.Writer); tw != nil {
err = tw.Flush()
}
return err
}
// Fprint "pretty-prints" an HCL node to output
2015-10-03 00:30:57 +00:00
// It calls Config.Fprint with default settings.
func Fprint(output io.Writer, node ast.Node) error {
2015-10-25 13:08:09 +00:00
return (&Config{}).Fprint(output, node)
2015-10-03 00:30:57 +00:00
}