Add colorized fields

This commit is contained in:
Louis Thibault 2019-03-26 20:20:51 +00:00
parent dae0fa8d5b
commit 7f1a2b9bfc
1 changed files with 23 additions and 2 deletions

View File

@ -280,9 +280,30 @@ func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
stringVal = fmt.Sprint(value)
}
var ca ColorApplicator = ApplicatorFunc(func(s string) string { return s })
if c, ok := value.(Colorizer); ok {
ca = c.Color()
}
if !f.needsQuoting(stringVal) {
b.WriteString(stringVal)
b.WriteString(ca.Apply(stringVal))
} else {
b.WriteString(fmt.Sprintf("%q", stringVal))
b.WriteString(ca.Apply(fmt.Sprintf("%q", stringVal)))
}
}
// ColorApplicator applies colors to a string
type ColorApplicator interface {
Apply(string) string
}
// Colorizer provides a colorized string-representation of the concrete data structure.
type Colorizer interface {
Color() ColorApplicator
}
// ApplicatorFunc turns a function into a ColorApplicator
type ApplicatorFunc func(string) string
// Apply color
func (f ApplicatorFunc) Apply(s string) string { return f(s) }