add config option HighlightValues for adding colour/color to output

This commit is contained in:
Geraint Edwards 2019-01-17 13:08:37 +00:00
parent d8f796af33
commit cf75182434
No known key found for this signature in database
GPG Key ID: 662262F0980FAEAC
5 changed files with 77 additions and 0 deletions

View File

@ -62,6 +62,11 @@ var (
closeMapBytes = []byte("]") closeMapBytes = []byte("]")
lenEqualsBytes = []byte("len=") lenEqualsBytes = []byte("len=")
capEqualsBytes = []byte("cap=") capEqualsBytes = []byte("cap=")
highlight1StartBytes = []byte("\x1b[32m")
highlight2StartBytes = []byte("\x1b[33m")
highlight3StartBytes = []byte("\x1b[34m")
highlight4StartBytes = []byte("\x1b[36m")
highlightEndBytes = []byte("\x1b[0m")
) )
// hexDigits is used to map a decimal value to a hex digit. // hexDigits is used to map a decimal value to a hex digit.

View File

@ -98,6 +98,10 @@ type ConfigState struct {
// be spewed to strings and sorted by those strings. This is only // be spewed to strings and sorted by those strings. This is only
// considered if SortKeys is true. // considered if SortKeys is true.
SpewKeys bool SpewKeys bool
// HighlightValues adds colour/color to scalar values in output.
// The colours are suitable for ANSI displays.
HighlightValues bool
} }
// Config is the active configuration of the top-level functions. // Config is the active configuration of the top-level functions.

View File

@ -118,6 +118,10 @@ The following configuration options are available:
spewed to strings and sorted by those strings. This is only spewed to strings and sorted by those strings. This is only
considered if SortKeys is true. considered if SortKeys is true.
* HighlightValues
When true, values in dumps are highlighted using colours/colors
suitable for ANSI-compatible displays.
Dump Usage Dump Usage
Simply call spew.Dump with a list of variables you want to dump: Simply call spew.Dump with a list of variables you want to dump:

View File

@ -309,6 +309,26 @@ func (d *dumpState) dump(v reflect.Value) {
} }
} }
highlightIsOn := false
if d.cs.HighlightValues {
switch kind {
case reflect.String:
highlightIsOn = true
d.w.Write(highlight1StartBytes)
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
highlightIsOn = true
d.w.Write(highlight2StartBytes)
case reflect.Bool:
highlightIsOn = true
d.w.Write(highlight3StartBytes)
case reflect.Uintptr:
highlightIsOn = true
d.w.Write(highlight4StartBytes)
}
}
switch kind { switch kind {
case reflect.Invalid: case reflect.Invalid:
// Do nothing. We should never get here since invalid has already // Do nothing. We should never get here since invalid has already
@ -446,6 +466,10 @@ func (d *dumpState) dump(v reflect.Value) {
fmt.Fprintf(d.w, "%v", v.String()) fmt.Fprintf(d.w, "%v", v.String())
} }
} }
if highlightIsOn {
d.w.Write(highlightEndBytes)
}
} }
// fdump is a helper function to consolidate the logic from the various public // fdump is a helper function to consolidate the logic from the various public

View File

@ -1040,3 +1040,43 @@ func TestDumpSortedKeys(t *testing.T) {
} }
} }
func TestDumpHighlightValues(t *testing.T) {
cfg := spew.ConfigState{HighlightValues: true}
colBytes := map[string]string{
"reset": "\x1b[0m",
"str": "\x1b[32m",
"num": "\x1b[33m",
"bool": "\x1b[34m",
"other": "\x1b[36m",
}
s := cfg.Sdump(map[int]string{1: "1", 3: "3", 2: "2"})
expected := "(map[int]string) (len=3) {\n" +
"(int) " + colBytes["num"] + "1" + colBytes["reset"] + ": (string) (len=1) " + colBytes["str"] + "\"1\"" + colBytes["reset"] + ",\n" +
"(int) " + colBytes["num"] + "3" + colBytes["reset"] + ": (string) (len=1) " + colBytes["str"] + "\"3\"" + colBytes["reset"] + ",\n" +
"(int) " + colBytes["num"] + "2" + colBytes["reset"] + ": (string) (len=1) " + colBytes["str"] + "\"2\"" + colBytes["reset"] + "\n" +
"}\n"
if s != expected {
t.Errorf("Highlighted string mismatch:\n %v %v", s, expected)
}
s = cfg.Sdump(map[stringer]int{"1": 1, "3": 3, "2": 2})
expected = "(map[spew_test.stringer]int) (len=3) {\n" +
"(spew_test.stringer) (len=1) stringer 1: (int) " + colBytes["num"] + "1" + colBytes["reset"] + ",\n" +
"(spew_test.stringer) (len=1) stringer 3: (int) " + colBytes["num"] + "3" + colBytes["reset"] + ",\n" +
"(spew_test.stringer) (len=1) stringer 2: (int) " + colBytes["num"] + "2" + colBytes["reset"] + "\n" +
"}\n"
if s != expected {
t.Errorf("Highlighted ints mismatch:\n %v %v", s, expected)
}
s = cfg.Sdump(map[string]bool{"custom1": true, "custom2": false})
expected = "(map[string]bool) (len=2) {\n" +
"(string) (len=7) " + colBytes["str"] + `"custom1"` + colBytes["reset"] + ": (bool) " + colBytes["bool"] + "true" + colBytes["reset"] + ",\n" +
"(string) (len=7) " + colBytes["str"] + `"custom2"` + colBytes["reset"] + ": (bool) " + colBytes["bool"] + "false" + colBytes["reset"] + "\n" +
"}\n"
if s != expected {
t.Errorf("Highlighted keys mismatch:\n %v %v", s, expected)
}
}