Refactoring Yaml encoder prefs

This commit is contained in:
Mike Farah
2024-02-24 15:36:16 +11:00
parent f44d47a204
commit 7a01e216c4
21 changed files with 103 additions and 86 deletions
+6 -11
View File
@@ -12,16 +12,11 @@ import (
)
type yamlEncoder struct {
indent int
colorise bool
prefs YamlPreferences
prefs YamlPreferences
}
func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder {
if indent < 0 {
indent = 0
}
return &yamlEncoder{indent, colorise, prefs}
func NewYamlEncoder(prefs YamlPreferences) Encoder {
return &yamlEncoder{prefs}
}
func (ye *yamlEncoder) CanHandleAliases() bool {
@@ -90,13 +85,13 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
destination := writer
tempBuffer := bytes.NewBuffer(nil)
if ye.colorise {
if ye.prefs.ColorsEnabled {
destination = tempBuffer
}
var encoder = yaml.NewEncoder(destination)
encoder.SetIndent(ye.indent)
encoder.SetIndent(ye.prefs.Indent)
target, err := node.MarshalYAML()
@@ -115,7 +110,7 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
return err
}
if ye.colorise {
if ye.prefs.ColorsEnabled {
return colorizeAndPrint(tempBuffer.Bytes(), writer)
}
return nil