Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text) escape value before rendering styles #20

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion text.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ func (l *logger) textFormatter(keyvals ...interface{}) {
} else {
key = KeyStyle.Render(key)
}
}
if !strings.Contains(val, "\n") && !raw && needsQuoting(val) {
b := &bytes.Buffer{}
writeEscapedForOutput(b, val, true)
val = b.String()
}
if !l.noStyles {
val = ValueStyle.Render(val)
}

Expand All @@ -229,7 +236,7 @@ func (l *logger) textFormatter(keyvals ...interface{}) {
l.b.WriteString(key)
l.b.WriteString(sep)
l.b.WriteByte('"')
writeEscapedForOutput(&l.b, val, true)
l.b.WriteString(val)
l.b.WriteByte('"')
} else {
l.b.WriteByte(' ')
Expand Down
76 changes: 76 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -228,3 +229,78 @@ func TestTextFatal(t *testing.T) {
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

func TestTextValueStyles(t *testing.T) {
var buf bytes.Buffer
logger := New(WithOutput(&buf)).(*logger)
logger.noStyles = false
ValueStyle = lipgloss.NewStyle().Bold(true)
cases := []struct {
name string
expected string
msg string
kvs []interface{}
f func(msg interface{}, kvs ...interface{})
}{
tombell marked this conversation as resolved.
Show resolved Hide resolved
{
name: "simple message",
expected: fmt.Sprintf("%s info\n", InfoLevelStyle.Render("INFO")),
msg: "info",
kvs: nil,
f: logger.Info,
},
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s\"%s\" %s%s\"%s\"\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("val1"),
KeyStyle.Render("key2"), SeparatorStyle.Render("="), ValueStyle.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1", "key2", "val2"},
f: logger.Info,
},
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s\"%s\" %s%s\"%s\"\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("true"),
KeyStyle.Render("key2"), SeparatorStyle.Render("="), ValueStyle.Render("false"),
),
msg: "info",
kvs: []interface{}{"key1", true, "key2", false},
f: logger.Info,
},
{
name: "struct field",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("{foo:bar}"),
),
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar"}},
f: logger.Info,
},
{
name: "struct field quoted",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("{foo:bar baz}"),
),
msg: "info",
kvs: []interface{}{"key1", struct{ foo string }{foo: "bar baz"}},
f: logger.Info,
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
c.f(c.msg, c.kvs...)
assert.Equal(t, c.expected, buf.String())
})
}
}