@@ -26,29 +26,66 @@ import (
2626 "fmt"
2727 "strings"
2828
29- colorful "github.com/lucasb-eyer/go-colorful"
3029 yamlv2 "gopkg.in/yaml.v2"
3130 yamlv3 "gopkg.in/yaml.v3"
3231
32+ "github.com/lucasb-eyer/go-colorful"
33+
3334 "github.com/gonvenience/bunt"
3435)
3536
37+ // frequently used output constants
38+ const (
39+ colorAnchor = "anchorColor"
40+ colorBinary = "binaryColor"
41+ colorBool = "boolColor"
42+ colorComment = "commentColor"
43+ colorDash = "dashColor"
44+ colorFloat = "floatColor"
45+ colorIndentLine = "indentLineColor"
46+ colorInt = "intColor"
47+ colorKey = "keyColor"
48+ colorMultiLineText = "multiLineTextColor"
49+ colorNull = "nullColor"
50+ colorScalarDefault = "scalarDefaultColor"
51+ )
52+
53+ const (
54+ documentStart = "documentStart"
55+ emptyStructures = "emptyStructures"
56+ )
57+
58+ const (
59+ emptyList = "[]"
60+ emptyObject = "{}"
61+ )
62+
63+ const (
64+ nodeTagBinary = "!!binary"
65+ nodeTagBool = "!!bool"
66+ nodeTagFloat = "!!float"
67+ nodeTagInt = "!!int"
68+ nodeTagNull = "!!null"
69+ nodeTagString = "!!str"
70+ nodeTagTime = "!!timestamp"
71+ )
72+
3673// DefaultColorSchema is a prepared usable color schema for the neat output
3774// processor which is loosly based upon the colors used by Atom
3875var DefaultColorSchema = map [string ]colorful.Color {
39- " documentStart" : bunt .LightSlateGray ,
40- "keyColor" : bunt .IndianRed ,
41- "indentLineColor" : {R : 0.14 , G : 0.14 , B : 0.14 },
42- "scalarDefaultColor" : bunt .PaleGreen ,
43- "boolColor" : bunt .Moccasin ,
44- "floatColor" : bunt .Orange ,
45- "intColor" : bunt .MediumPurple ,
46- "multiLineTextColor" : bunt .Aquamarine ,
47- "nullColor" : bunt .DarkOrange ,
48- "binaryColor" : bunt .Aqua ,
49- " emptyStructures" : bunt .PaleGoldenrod ,
50- "commentColor" : bunt .DimGray ,
51- "anchorColor" : bunt .CornflowerBlue ,
76+ documentStart : bunt .LightSlateGray ,
77+ colorKey : bunt .IndianRed ,
78+ colorIndentLine : {R : 0.14 , G : 0.14 , B : 0.14 },
79+ colorScalarDefault : bunt .PaleGreen ,
80+ colorBool : bunt .Moccasin ,
81+ colorFloat : bunt .Orange ,
82+ colorInt : bunt .MediumPurple ,
83+ colorMultiLineText : bunt .Aquamarine ,
84+ colorNull : bunt .DarkOrange ,
85+ colorBinary : bunt .Aqua ,
86+ emptyStructures : bunt .PaleGoldenrod ,
87+ colorComment : bunt .DimGray ,
88+ colorAnchor : bunt .CornflowerBlue ,
5289}
5390
5491// OutputProcessor provides the functionality to output neat YAML strings using
@@ -81,8 +118,8 @@ func NewOutputProcessor(useIndentLines bool, boldKeys bool, colorSchema *map[str
81118 }
82119}
83120
84- // TODO Change signature to swap into the right order, color first, text second
85- func (p * OutputProcessor ) colorize (text string , colorName string ) string {
121+ // colorize returns the given string with the color applied via bunt.
122+ func (p * OutputProcessor ) colorize (colorName string , text string ) string {
86123 if p .colorSchema != nil {
87124 if value , ok := (* p .colorSchema )[colorName ]; ok {
88125 return bunt .Style (text , bunt .Foreground (value ))
@@ -92,60 +129,64 @@ func (p *OutputProcessor) colorize(text string, colorName string) string {
92129 return text
93130}
94131
132+ // colorizef formats a string using the provided color name and format string (created via fmt.Sprintf)
133+ // and returns the formatted string with the color applied via bunt.
134+ //
135+ // If additional arguments are not provided, the function skips the fmt.Sprintf call.
95136func (p * OutputProcessor ) colorizef (colorName string , format string , a ... interface {}) string {
96137 if len (a ) > 0 {
97- return p .colorize (fmt .Sprintf (format , a ... ), colorName )
138+ return p .colorize (colorName , fmt .Sprintf (format , a ... ))
98139 }
99140
100- return p .colorize (format , colorName )
141+ return p .colorize (colorName , format )
101142}
102143
103144func (p * OutputProcessor ) determineColorByType (obj interface {}) string {
104- color := "scalarDefaultColor"
145+ color := colorScalarDefault
105146
106147 switch t := obj .(type ) {
107148 case * yamlv3.Node :
108149 switch t .Tag {
109- case "!!str" :
150+ case nodeTagString :
110151 if len (strings .Split (strings .TrimSpace (t .Value ), "\n " )) > 1 {
111- color = "multiLineTextColor"
152+ color = colorMultiLineText
112153 }
113154
114- case "!!int" :
115- color = "intColor"
155+ case nodeTagInt :
156+ color = colorInt
116157
117- case "!!float" :
118- color = "floatColor"
158+ case nodeTagFloat :
159+ color = colorFloat
119160
120- case "!!bool" :
121- color = "boolColor"
161+ case nodeTagBool :
162+ color = colorBool
122163
123- case "!!null" :
124- color = "nullColor"
164+ case nodeTagNull :
165+ color = colorNull
125166 }
126167
127168 case bool :
128- color = "boolColor"
169+ color = colorBool
129170
130171 case float32 , float64 :
131- color = "floatColor"
172+ color = colorFloat
132173
133174 case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , uintptr :
134- color = "intColor"
175+ color = colorInt
135176
136177 case string :
137178 if len (strings .Split (strings .TrimSpace (t ), "\n " )) > 1 {
138- color = "multiLineTextColor"
179+ color = colorMultiLineText
139180 }
140181 }
141182
142183 return color
143184}
144185
145186func (p * OutputProcessor ) isScalar (obj interface {}) bool {
146- switch tobj := obj .(type ) {
187+ switch tObj := obj .(type ) {
147188 case * yamlv3.Node :
148- return tobj .Kind == yamlv3 .ScalarNode
189+ return tObj .Kind == yamlv3 .ScalarNode
149190
150191 case yamlv2.MapSlice , []interface {}, []yamlv2.MapSlice :
151192 return false
@@ -166,10 +207,10 @@ func (p *OutputProcessor) simplify(list []yamlv2.MapSlice) []interface{} {
166207
167208func (p * OutputProcessor ) prefixAdd () string {
168209 if p .useIndentLines {
169- return p .colorize ("│ " , "indentLineColor " )
210+ return p .colorize (colorIndentLine , "│ " )
170211 }
171212
172- return p .colorize (" " , "indentLineColor " )
213+ return p .colorize (colorIndentLine , " " )
173214}
174215
175216func followAlias (node * yamlv3.Node ) * yamlv3.Node {
0 commit comments