Skip to content

Commit

Permalink
Merge pull request #21 from hermannm/textmarshaler-colorization
Browse files Browse the repository at this point in the history
Implement colorization for encoding.TextMarshaler
  • Loading branch information
neilotoole authored Nov 10, 2023
2 parents 1dfcb2a + a121c4f commit 09171c9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
11 changes: 9 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ func (e encoder) encodeTextMarshaler(b []byte, p unsafe.Pointer, t reflect.Type,
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return append(b, `null`...), nil
return e.clrs.appendNull(b), nil
}
}

Expand All @@ -920,7 +920,14 @@ func (e encoder) encodeTextMarshaler(b []byte, p unsafe.Pointer, t reflect.Type,
return b, err
}

return e.doEncodeString(b, unsafe.Pointer(&s))
if e.clrs == nil {
return e.doEncodeString(b, unsafe.Pointer(&s))
}

b = append(b, e.clrs.TextMarshaler...)
b, err = e.doEncodeString(b, unsafe.Pointer(&s))
b = append(b, ansiReset...)
return b, err
}

func appendCompactEscapeHTML(dst []byte, src []byte) []byte {
Expand Down
37 changes: 21 additions & 16 deletions helper/fatihcolor/fatihcolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,23 @@ type Colors struct {
// Punc is the color for punctuation such as colons, braces, etc.
// Frequently Punc will just be color.Bold.
Punc *color.Color

// TextMarshaler is the color for types implementing encoding.TextMarshaler.
TextMarshaler *color.Color
}

// DefaultColors returns default Colors instance.
func DefaultColors() *Colors {
return &Colors{
Bool: color.New(color.FgYellow),
Bytes: color.New(color.Faint),
Datetime: color.New(color.FgGreen, color.Faint),
Key: color.New(color.FgBlue, color.Bold),
Null: color.New(color.Faint),
Number: color.New(color.FgCyan),
String: color.New(color.FgGreen),
Punc: color.New(color.Bold),
Bool: color.New(color.FgYellow),
Bytes: color.New(color.Faint),
Datetime: color.New(color.FgGreen, color.Faint),
Key: color.New(color.FgBlue, color.Bold),
Null: color.New(color.Faint),
Number: color.New(color.FgCyan),
String: color.New(color.FgGreen),
Punc: color.New(color.Bold),
TextMarshaler: color.New(color.FgGreen),
}
}

Expand All @@ -60,14 +64,15 @@ func ToCoreColors(clrs *Colors) *jsoncolor.Colors {
}

return &jsoncolor.Colors{
Null: ToCoreColor(clrs.Null),
Bool: ToCoreColor(clrs.Bool),
Number: ToCoreColor(clrs.Number),
String: ToCoreColor(clrs.String),
Key: ToCoreColor(clrs.Key),
Bytes: ToCoreColor(clrs.Bytes),
Time: ToCoreColor(clrs.Datetime),
Punc: ToCoreColor(clrs.Punc),
Null: ToCoreColor(clrs.Null),
Bool: ToCoreColor(clrs.Bool),
Number: ToCoreColor(clrs.Number),
String: ToCoreColor(clrs.String),
Key: ToCoreColor(clrs.Key),
Bytes: ToCoreColor(clrs.Bytes),
Time: ToCoreColor(clrs.Datetime),
Punc: ToCoreColor(clrs.Punc),
TextMarshaler: ToCoreColor(clrs.TextMarshaler),
}
}

Expand Down
20 changes: 12 additions & 8 deletions jsoncolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Colors struct {

// Punc is the color for JSON punctuation: []{},: etc.
Punc Color

// TextMarshaler is the color for values implementing encoding.TextMarshaler.
TextMarshaler Color
}

// appendNull appends a colorized "null" to b.
Expand Down Expand Up @@ -131,14 +134,15 @@ const ansiReset = "\x1b[0m"
// with some deviation.
func DefaultColors() *Colors {
return &Colors{
Null: Color("\x1b[2m"),
Bool: Color("\x1b[1m"),
Number: Color("\x1b[36m"),
String: Color("\x1b[32m"),
Key: Color("\x1b[34;1m"),
Bytes: Color("\x1b[2m"),
Time: Color("\x1b[32;2m"),
Punc: Color{}, // No colorization
Null: Color("\x1b[2m"),
Bool: Color("\x1b[1m"),
Number: Color("\x1b[36m"),
String: Color("\x1b[32m"),
Key: Color("\x1b[34;1m"),
Bytes: Color("\x1b[2m"),
Time: Color("\x1b[32;2m"),
Punc: Color{}, // No colorization
TextMarshaler: Color("\x1b[32m"), // Same as String
}
}

Expand Down
23 changes: 23 additions & 0 deletions jsoncolor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,26 @@ func TestEquivalenceRecords(t *testing.T) {
err = jsoncolor.NewEncoder(bufJ).Encode(rec)
require.Equal(t, bufStdj.String(), bufJ.String())
}

// TextMarshaler implements encoding.TextMarshaler
type TextMarshaler struct {
Text string
}

func (t TextMarshaler) MarshalText() ([]byte, error) {
return []byte(t.Text), nil
}

func TestEncode_TextMarshaler(t *testing.T) {
buf := &bytes.Buffer{}
enc := jsoncolor.NewEncoder(buf)
enc.SetColors(&jsoncolor.Colors{
TextMarshaler: jsoncolor.Color("\x1b[36m"),
})

text := TextMarshaler{Text: "example text"}

require.NoError(t, enc.Encode(text))
require.Equal(t, "\x1b[36m\"example text\"\x1b[0m\n", buf.String(),
"expected TextMarshaler encoding to use Colors.TextMarshaler")
}

0 comments on commit 09171c9

Please sign in to comment.