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
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
Prev Previous commit
Next Next commit
fix(text) render value style before escaping
tombell committed Feb 24, 2023
commit 32448a9425e0b55e9d24d5961c259205f9f9e914
33 changes: 18 additions & 15 deletions text.go
Original file line number Diff line number Diff line change
@@ -16,14 +16,14 @@ const (
indentSeparator = " │ "
)

func writeIndent(w io.Writer, str string, indent string, newline bool) {
func (l *logger) writeIndent(w io.Writer, str string, indent string, newline bool) {
// kindly borrowed from hclog
for {
nl := strings.IndexByte(str, '\n')
if nl == -1 {
if str != "" {
_, _ = w.Write([]byte(indent))
writeEscapedForOutput(w, str, false)
l.writeEscapedForOutput(w, str, false)
if newline {
_, _ = w.Write([]byte{'\n'})
}
@@ -32,7 +32,7 @@ func writeIndent(w io.Writer, str string, indent string, newline bool) {
}

_, _ = w.Write([]byte(indent))
writeEscapedForOutput(w, str[:nl], false)
l.writeEscapedForOutput(w, str[:nl], false)
_, _ = w.Write([]byte{'\n'})
str = str[nl+1:]
}
@@ -58,9 +58,12 @@ var bufPool = sync.Pool{
},
}

func writeEscapedForOutput(w io.Writer, str string, escapeQuotes bool) {
func (l *logger) writeEscapedForOutput(w io.Writer, str string, escapeQuotes bool) {
// kindly borrowed from hclog
if !needsEscaping(str) {
if !l.noStyles {
str = ValueStyle.Render(str)
}
_, _ = w.Write([]byte(str))
return
}
@@ -115,7 +118,12 @@ func writeEscapedForOutput(w io.Writer, str string, escapeQuotes bool) {
}
}

_, _ = w.Write(bb.Bytes())
s := bb.String()
if !l.noStyles {
s = ValueStyle.Render(s)
}

_, _ = w.Write([]byte(s))
}

// isNormal indicates if the rune is one allowed to exist as an unquoted
@@ -206,14 +214,6 @@ func (l *logger) textFormatter(keyvals ...interface{}) {
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)
}

// Values may contain multiple lines, and that format
// is preserved, with each line prefixed with a " | "
@@ -226,7 +226,7 @@ func (l *logger) textFormatter(keyvals ...interface{}) {
l.b.WriteString("\n ")
l.b.WriteString(key)
l.b.WriteString(sep + "\n")
writeIndent(&l.b, val, indentSep, moreKeys)
l.writeIndent(&l.b, val, indentSep, moreKeys)
// If there are more keyvals, separate them with a space.
if moreKeys {
l.b.WriteByte(' ')
@@ -236,9 +236,12 @@ func (l *logger) textFormatter(keyvals ...interface{}) {
l.b.WriteString(key)
l.b.WriteString(sep)
l.b.WriteByte('"')
l.b.WriteString(val)
l.writeEscapedForOutput(&l.b, val, true)
l.b.WriteByte('"')
} else {
if !l.noStyles {
val = ValueStyle.Render(val)
}
l.b.WriteByte(' ')
l.b.WriteString(key)
l.b.WriteString(sep)
19 changes: 16 additions & 3 deletions text_test.go
Original file line number Diff line number Diff line change
@@ -252,7 +252,7 @@ func TestTextValueStyles(t *testing.T) {
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s\"%s\" %s%s\"%s\"\n",
"%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"),
@@ -264,7 +264,7 @@ func TestTextValueStyles(t *testing.T) {
{
name: "message with keyvals",
expected: fmt.Sprintf(
"%s info %s%s\"%s\" %s%s\"%s\"\n",
"%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"),
@@ -273,10 +273,23 @@ func TestTextValueStyles(t *testing.T) {
kvs: []interface{}{"key1", true, "key2", false},
f: logger.Info,
},
{
name: "error message with multiline",
expected: fmt.Sprintf(
"%s info\n %s%s\n%s%s\n%s%s\n",
ErrorLevelStyle.Render("ERRO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="),
SeparatorStyle.Render(" │ "), ValueStyle.Render("val1"),
SeparatorStyle.Render(" │ "), ValueStyle.Render("val2"),
),
msg: "info",
kvs: []interface{}{"key1", "val1\nval2"},
f: logger.Error,
},
{
name: "struct field",
expected: fmt.Sprintf(
"%s info %s%s\"%s\"\n",
"%s info %s%s%s\n",
InfoLevelStyle.Render("INFO"),
KeyStyle.Render("key1"), SeparatorStyle.Render("="), ValueStyle.Render("{foo:bar}"),
),