Skip to content

Commit

Permalink
console backquote quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Aug 21, 2024
1 parent c587f8f commit 471a1d4
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ type (
PairSeparator string
KVSeparator string

QuoteChars string
QuoteAnyValue bool
QuoteEmptyValue bool
QuoteChars string
QuoteAnyValue bool
QuoteEmptyValue bool
QuoteUseBackQuotes bool

ColorScheme

Expand Down Expand Up @@ -175,8 +176,9 @@ func NewConsoleWriter(w io.Writer, f int) *ConsoleWriter {
PairSeparator: " ",
KVSeparator: "=",

QuoteChars: "`\"' ()[]{}*",
QuoteEmptyValue: true,
QuoteChars: "`\"' ()[]{}*",
QuoteEmptyValue: true,
QuoteUseBackQuotes: true,

ColorScheme: DefaultColorScheme,

Expand Down Expand Up @@ -695,7 +697,7 @@ func (w *ConsoleWriter) ConvertValue(b, p []byte, st, ff int) (_ []byte, i int)
}

if ff&cfHex != 0 {
b = hfmt.Appendf(b, "[% 02x]", s)
b = w.AppendBytes(b, s)
break
}
}
Expand All @@ -705,22 +707,31 @@ func (w *ConsoleWriter) ConvertValue(b, p []byte, st, ff int) (_ []byte, i int)
}

quote := tag == tlwire.Bytes || w.QuoteAnyValue || len(s) == 0 && w.QuoteEmptyValue
haveQuotes, haveBackQuotes := false, false
if !quote {
for _, c := range s {
if c < 0x20 || c >= 0x80 {
quote = true
break
}

for _, q := range w.QuoteChars {
if byte(q) == c {
quote = true
break
}
}

haveQuotes = haveQuotes || c == '"'
haveBackQuotes = haveBackQuotes || c == '`'
}
}

if quote {
if quote && haveQuotes && !haveBackQuotes && w.QuoteUseBackQuotes {

Check failure on line 730 in console.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)
b = append(b, '`')
b = append(b, s...)
b = append(b, '`')
} else if quote {
ss := tlow.UnsafeBytesToString(s)
b = strconv.AppendQuote(b, ss)
} else {
Expand Down Expand Up @@ -988,6 +999,24 @@ func (w *ConsoleWriter) AppendDuration(b []byte, d time.Duration) []byte {
return append(b, buf[i:j]...)
}

func (w *ConsoleWriter) AppendBytes(b, s []byte) []byte {
const hex = "0123456789abcdef"

Check failure on line 1003 in console.go

View workflow job for this annotation

GitHub Actions / lint

importShadow: shadow of imported package 'hex' (gocritic)

b = append(b, '[')

for i, c := range s {
if i != 0 {
b = append(b, ' ')
}

b = append(b, hex[c>>4], hex[c&0xf])
}

b = append(b, ']')

return b
}

func width(n int) (w int) {
q := 10
w = 1
Expand Down

0 comments on commit 471a1d4

Please sign in to comment.