Skip to content

Commit 9801b52

Browse files
committed
deduplicating code in diff/output.go
1 parent 959328e commit 9801b52

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

diff/output.go

+14-32
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,23 @@ type Output struct {
1616
JSONValues bool
1717
}
1818

19-
func (o Output) red(v interface{}) string {
20-
var s string
19+
type colorFn func(format string, a ...interface{}) string
2120

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
3422

35-
return color.RedString("%s", s)
23+
func (o Output) red(v interface{}) string {
24+
return o.applyColor(v, color.RedString)
3625
}
3726

3827
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)
5529
}
5630

5731
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 {
5836
var s string
5937

6038
switch {
@@ -66,7 +44,11 @@ func (o Output) white(v interface{}) string {
6644
s = jsonString(v)
6745
}
6846

69-
return s
47+
if !o.Colorized || fn == nil {
48+
return s
49+
}
50+
51+
return fn("%s", s)
7052
}
7153

7254
func (o Output) typ(v interface{}) string {

0 commit comments

Comments
 (0)