hcl/printer/printer.go

53 lines
992 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 {
out []byte // raw printer result
cfg Config
node ast.Node
2015-10-03 00:30:57 +00:00
}
func (p *printer) output() []byte {
return p.printNode(p.node)
2015-10-03 00:30:57 +00:00
}
// A Config node controls the output of Fprint.
type Config struct {
2015-10-25 13:08:09 +00:00
SpaceWidth 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,
node: node,
2015-10-03 00:30:57 +00:00
}
if _, err := output.Write(p.output()); err != nil {
return err
}
// flush tabwriter, if any
var err error
if tw, _ := output.(*tabwriter.Writer); tw != nil {
err = tw.Flush()
}
return err
}
func (c *Config) Fprint(output io.Writer, node ast.Node) error {
return c.fprint(output, node)
2015-10-03 00:30:57 +00:00
}
// 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
}