Skip to content

Commit

Permalink
fix(text) render value style before escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
tombell committed Feb 24, 2023
1 parent e7a92ba commit 32448a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
33 changes: 18 additions & 15 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
}
Expand All @@ -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:]
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 " | "
Expand All @@ -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(' ')
Expand All @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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}"),
),
Expand Down

0 comments on commit 32448a9

Please sign in to comment.