@@ -16,45 +16,23 @@ type Output struct {
16
16
JSONValues bool
17
17
}
18
18
19
- func (o Output ) red (v interface {}) string {
20
- var s string
19
+ type colorFn func (format string , a ... interface {}) string
21
20
22
- switch {
23
- default :
24
- s = fmt .Sprintf ("%v" , v )
25
- case o .ShowTypes :
26
- s = fmt .Sprintf ("%T %v" , v , v )
27
- case o .JSONValues :
28
- s = jsonString (v )
29
- }
30
-
31
- if ! o .Colorized {
32
- return s
33
- }
21
+ var whiteFn colorFn = nil
34
22
35
- return color .RedString ("%s" , s )
23
+ func (o Output ) red (v interface {}) string {
24
+ return o .applyColor (v , color .RedString )
36
25
}
37
26
38
27
func (o Output ) green (v interface {}) string {
39
- var s string
40
-
41
- switch {
42
- default :
43
- s = fmt .Sprintf ("%v" , v )
44
- case o .ShowTypes :
45
- s = fmt .Sprintf ("%T %v" , v , v )
46
- case o .JSONValues :
47
- s = jsonString (v )
48
- }
49
-
50
- if ! o .Colorized {
51
- return s
52
- }
53
-
54
- return color .GreenString ("%s" , s )
28
+ return o .applyColor (v , color .GreenString )
55
29
}
56
30
57
31
func (o Output ) white (v interface {}) string {
32
+ return o .applyColor (v , whiteFn )
33
+ }
34
+
35
+ func (o Output ) applyColor (v interface {}, fn colorFn ) string {
58
36
var s string
59
37
60
38
switch {
@@ -66,7 +44,11 @@ func (o Output) white(v interface{}) string {
66
44
s = jsonString (v )
67
45
}
68
46
69
- return s
47
+ if ! o .Colorized || fn == nil {
48
+ return s
49
+ }
50
+
51
+ return fn ("%s" , s )
70
52
}
71
53
72
54
func (o Output ) typ (v interface {}) string {
0 commit comments