@@ -26,29 +26,61 @@ 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+
39+ const (
40+ colorAnchor = "anchorColor"
41+ colorBinary = "binaryColor"
42+ colorBool = "boolColor"
43+ colorComment = "commentColor"
44+ colorDash = "dashColor"
45+ colorFloat = "floatColor"
46+ colorIndentLine = "indentLineColor"
47+ colorInt = "intColor"
48+ colorKey = "keyColor"
49+ colorMultiLineText = "multiLineTextColor"
50+ colorNull = "nullColor"
51+ colorScalarDefault = "scalarDefaultColor"
52+
53+ documentStart = "documentStart"
54+ emptyStructures = "emptyStructures"
55+
56+ emptyList = "[]"
57+ emptyObject = "{}"
58+
59+ nodeTagBinary = "!!binary"
60+ nodeTagBool = "!!bool"
61+ nodeTagFloat = "!!float"
62+ nodeTagInt = "!!int"
63+ nodeTagNull = "!!null"
64+ nodeTagString = "!!str"
65+ nodeTagTime = "!!timestamp"
66+ )
67+
3668// DefaultColorSchema is a prepared usable color schema for the neat output
3769// processor which is loosly based upon the colors used by Atom
3870var 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 ,
71+ documentStart : bunt .LightSlateGray ,
72+ colorKey : bunt .IndianRed ,
73+ colorIndentLine : {R : 0.14 , G : 0.14 , B : 0.14 },
74+ colorScalarDefault : bunt .PaleGreen ,
75+ colorBool : bunt .Moccasin ,
76+ colorFloat : bunt .Orange ,
77+ colorInt : bunt .MediumPurple ,
78+ colorMultiLineText : bunt .Aquamarine ,
79+ colorNull : bunt .DarkOrange ,
80+ colorBinary : bunt .Aqua ,
81+ emptyStructures : bunt .PaleGoldenrod ,
82+ colorComment : bunt .DimGray ,
83+ colorAnchor : bunt .CornflowerBlue ,
5284}
5385
5486// OutputProcessor provides the functionality to output neat YAML strings using
@@ -81,8 +113,8 @@ func NewOutputProcessor(useIndentLines bool, boldKeys bool, colorSchema *map[str
81113 }
82114}
83115
84- // TODO Change signature to swap into the right order, color first, text second
85- func (p * OutputProcessor ) colorize (text string , colorName string ) string {
116+ // colorize returns the given string with the color applied via bunt.
117+ func (p * OutputProcessor ) colorize (colorName string , text string ) string {
86118 if p .colorSchema != nil {
87119 if value , ok := (* p .colorSchema )[colorName ]; ok {
88120 return bunt .Style (text , bunt .Foreground (value ))
@@ -92,60 +124,64 @@ func (p *OutputProcessor) colorize(text string, colorName string) string {
92124 return text
93125}
94126
127+ // colorizef formats a string using the provided color name and format string (created via fmt.Sprintf)
128+ // and returns the formatted string with the color applied via bunt.
129+ //
130+ // If additional arguments are not provided, the function skips the fmt.Sprintf call.
95131func (p * OutputProcessor ) colorizef (colorName string , format string , a ... interface {}) string {
96132 if len (a ) > 0 {
97- return p .colorize (fmt .Sprintf (format , a ... ), colorName )
133+ return p .colorize (colorName , fmt .Sprintf (format , a ... ))
98134 }
99135
100- return p .colorize (format , colorName )
136+ return p .colorize (colorName , format )
101137}
102138
103139func (p * OutputProcessor ) determineColorByType (obj interface {}) string {
104- color := "scalarDefaultColor"
140+ color := colorScalarDefault
105141
106142 switch t := obj .(type ) {
107143 case * yamlv3.Node :
108144 switch t .Tag {
109- case "!!str" :
145+ case nodeTagString :
110146 if len (strings .Split (strings .TrimSpace (t .Value ), "\n " )) > 1 {
111- color = "multiLineTextColor"
147+ color = colorMultiLineText
112148 }
113149
114- case "!!int" :
115- color = "intColor"
150+ case nodeTagInt :
151+ color = colorInt
116152
117- case "!!float" :
118- color = "floatColor"
153+ case nodeTagFloat :
154+ color = colorFloat
119155
120- case "!!bool" :
121- color = "boolColor"
156+ case nodeTagBool :
157+ color = colorBool
122158
123- case "!!null" :
124- color = "nullColor"
159+ case nodeTagNull :
160+ color = colorNull
125161 }
126162
127163 case bool :
128- color = "boolColor"
164+ color = colorBool
129165
130166 case float32 , float64 :
131- color = "floatColor"
167+ color = colorFloat
132168
133169 case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , uintptr :
134- color = "intColor"
170+ color = colorInt
135171
136172 case string :
137173 if len (strings .Split (strings .TrimSpace (t ), "\n " )) > 1 {
138- color = "multiLineTextColor"
174+ color = colorMultiLineText
139175 }
140176 }
141177
142178 return color
143179}
144180
145181func (p * OutputProcessor ) isScalar (obj interface {}) bool {
146- switch tobj := obj .(type ) {
182+ switch tObj := obj .(type ) {
147183 case * yamlv3.Node :
148- return tobj .Kind == yamlv3 .ScalarNode
184+ return tObj .Kind == yamlv3 .ScalarNode
149185
150186 case yamlv2.MapSlice , []interface {}, []yamlv2.MapSlice :
151187 return false
@@ -166,10 +202,10 @@ func (p *OutputProcessor) simplify(list []yamlv2.MapSlice) []interface{} {
166202
167203func (p * OutputProcessor ) prefixAdd () string {
168204 if p .useIndentLines {
169- return p .colorize ( "│ " , "indentLineColor " )
205+ return p .colorizef ( colorIndentLine , "│ " )
170206 }
171207
172- return p .colorize ( " " , "indentLineColor " )
208+ return p .colorizef ( colorIndentLine , " " )
173209}
174210
175211func followAlias (node * yamlv3.Node ) * yamlv3.Node {
0 commit comments