Skip to content

Commit

Permalink
string indent as option
Browse files Browse the repository at this point in the history
  • Loading branch information
david-littlefarmer committed Dec 19, 2023
1 parent 2ff54a1 commit 5083e52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
20 changes: 13 additions & 7 deletions devslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())...)
Expand Down

0 comments on commit 5083e52

Please sign in to comment.