From 5083e52ef15955a1028b9aa7e77d65a227a30c9a Mon Sep 17 00:00:00 2001 From: david-littlefarmer Date: Tue, 19 Dec 2023 11:11:56 +0100 Subject: [PATCH] string indent as option --- README.md | 1 + devslog.go | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e4256ff..b7a116a 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ slog.SetDefault(logger) | SortKeys | Determines if attributes should be sorted by keys. | false | bool | | TimeFormat | Time format for timestamp. | "[15:04:05]" | string | | NewLineAfterLog | Add blank line after each log | false | bool | +| StringIndentation | Indent \n in strings | false | bool | | DebugColor | Color for Debug level | devslog.Blue | devslog.Color (uint) | | InfoColor | Color for Info level | devslog.Green | devslog.Color (uint) | | WarnColor | Color for Warn level | devslog.Yellow | devslog.Color (uint) | diff --git a/devslog.go b/devslog.go index e96542d..126d368 100644 --- a/devslog.go +++ b/devslog.go @@ -41,6 +41,9 @@ type Options struct { // Add blank line after each log NewLineAfterLog bool + // Indent \n in strings + StringIndentation bool + // Set color for Debug level, default: devslog.Blue DebugColor Color @@ -273,8 +276,10 @@ func (h *developHandler) colorize(b []byte, as attributes, l int, g []string) [] m = cs([]byte("*"), fgBlue) v = ul(cs(v, fgBlue)) } else { - count := l*2 + (4 + (pr)) - v = []byte(strings.ReplaceAll(string(v), "\n", "\n"+strings.Repeat(" ", count))) + if h.opts.StringIndentation { + count := l*2 + (4 + (pr)) + v = []byte(strings.ReplaceAll(string(v), "\n", "\n"+strings.Repeat(" ", count))) + } } case slog.KindTime, slog.KindDuration: @@ -459,7 +464,6 @@ func (h *developHandler) formatSlice(st reflect.Type, sv reflect.Value, l int) ( b = append(b, ':') b = append(b, ' ') b = append(b, h.elementType(t, v, l, l*2+d+2)...) - } return b @@ -554,13 +558,15 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i } else if h.isURL([]byte(s)) { b = ul(cs([]byte(s), fgBlue)) } else { - b = []byte(strings.ReplaceAll(string(s), "\n", "\n"+strings.Repeat(" ", l*2+p+4))) - - // b = atb(s) + if h.opts.StringIndentation { + b = []byte(strings.ReplaceAll(string(s), "\n", "\n"+strings.Repeat(" ", l*2+p+4))) + } else { + b = atb(s) + } } case reflect.Interface: v = reflect.ValueOf(v.Interface()) - b = h.elementType(v.Type(), v, l) + b = h.elementType(v.Type(), v, l, p) default: b = atb("Unknown type: ") b = append(b, atb(v.Kind())...)