Skip to content

Commit 7a0b805

Browse files
fix padding calc
1 parent 4d9b7e3 commit 7a0b805

File tree

3 files changed

+83
-96
lines changed

3 files changed

+83
-96
lines changed

attributes.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,3 @@ func (a attributes) Less(i, j int) bool {
1717

1818
return a[i].Key < a[j].Key
1919
}
20-
21-
func (a attributes) padding(c foregroundColor, colorFunction func(b []byte, fgColor foregroundColor) []byte) int {
22-
var padding int
23-
for _, e := range a {
24-
color := len(e.Key)
25-
if c != nil {
26-
color = len(colorFunction([]byte(e.Key), c))
27-
}
28-
29-
if color > padding {
30-
padding = color
31-
}
32-
}
33-
34-
return padding
35-
}

attributes_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func Test_Attributes(t *testing.T) {
1111
testAttributesLess(t)
1212
testAttributesLessGroupTrue(t)
1313
testAttributesLessGroupFalse(t)
14-
testAttributesPadding(t)
1514
}
1615

1716
func testAttributesLen(t *testing.T) {
@@ -84,19 +83,3 @@ func testAttributesLessGroupFalse(t *testing.T) {
8483
t.Error("Expected the first attribute to be less than the second")
8584
}
8685
}
87-
88-
func testAttributesPadding(t *testing.T) {
89-
someValue := slog.StringValue("value")
90-
attrs := attributes{
91-
slog.Attr{Key: "key1", Value: someValue},
92-
slog.Attr{Key: "key2", Value: someValue},
93-
}
94-
95-
h := NewHandler(nil, nil)
96-
padding := attrs.padding(fgMagenta, h.cs)
97-
98-
expectedPadding := 13
99-
if padding != expectedPadding {
100-
t.Errorf("Expected padding: %d, but got: %d", expectedPadding, padding)
101-
}
102-
}

devslog.go

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -274,151 +274,151 @@ func (h *developHandler) processAttributes(b []byte, r *slog.Record) []byte {
274274
return b
275275
}
276276

277-
func (h *developHandler) colorize(b []byte, as attributes, l int, g []string, vi visited) []byte {
277+
func (h *developHandler) colorize(b []byte, as attributes, l int, group []string, vi visited) []byte {
278278
if h.opts.SortKeys {
279279
sort.Sort(as)
280280
}
281281

282-
pr := as.padding(nil, h.cs)
283-
pc := as.padding(fgMagenta, h.cs)
282+
paddingNoColor := h.padding(as, group, nil, h.cs)
283+
paddingColor := h.padding(as, group, fgMagenta, h.cs)
284284
for _, a := range as {
285285
if h.opts.ReplaceAttr != nil {
286-
a = h.opts.ReplaceAttr(g, a)
286+
a = h.opts.ReplaceAttr(group, a)
287287
}
288288

289-
k := h.cs([]byte(a.Key), fgMagenta)
290-
v := []byte(a.Value.String())
291-
vo := v
292-
vs := v
293-
m := []byte(" ")
289+
key := h.cs([]byte(a.Key), fgMagenta)
290+
val := []byte(a.Value.String())
291+
valOld := val
292+
vs := val
293+
mark := []byte(" ")
294294

295295
switch a.Value.Kind() {
296296
case slog.KindFloat64, slog.KindInt64, slog.KindUint64:
297-
m = h.cs([]byte("#"), fgYellow)
298-
v = h.cs(v, fgYellow)
297+
mark = h.cs([]byte("#"), fgYellow)
298+
val = h.cs(val, fgYellow)
299299
case slog.KindBool:
300-
m = h.cs([]byte("#"), fgRed)
301-
v = h.cs(v, fgRed)
300+
mark = h.cs([]byte("#"), fgRed)
301+
val = h.cs(val, fgRed)
302302
case slog.KindString:
303-
if len(v) == 0 {
304-
v = h.csf([]byte("empty"), fgWhite)
305-
} else if h.isURL(v) {
306-
m = h.cs([]byte("*"), fgBlue)
307-
v = h.ul(h.cs(v, fgBlue))
303+
if len(val) == 0 {
304+
val = h.csf([]byte("empty"), fgWhite)
305+
} else if h.isURL(val) {
306+
mark = h.cs([]byte("*"), fgBlue)
307+
val = h.ul(h.cs(val, fgBlue))
308308
} else {
309309
if h.opts.StringIndentation {
310-
count := l*2 + (4 + (pr))
311-
v = []byte(strings.ReplaceAll(string(v), "\n", "\n"+strings.Repeat(" ", count)))
310+
count := l*2 + (4 + (paddingNoColor))
311+
val = []byte(strings.ReplaceAll(string(val), "\n", "\n"+strings.Repeat(" ", count)))
312312
}
313313
}
314314
case slog.KindTime, slog.KindDuration:
315-
m = h.cs([]byte("@"), fgCyan)
316-
v = h.cs(v, fgCyan)
315+
mark = h.cs([]byte("@"), fgCyan)
316+
val = h.cs(val, fgCyan)
317317
case slog.KindAny:
318318
av := a.Value.Any()
319319
if err, ok := av.(error); ok {
320-
m = h.cs([]byte("E"), fgRed)
321-
v = h.formatError(err, l)
320+
mark = h.cs([]byte("E"), fgRed)
321+
val = h.formatError(err, l)
322322
break
323323
}
324324

325325
if t, ok := av.(*time.Time); ok {
326-
m = h.cs([]byte("@"), fgCyan)
327-
v = h.cs([]byte(t.String()), fgCyan)
326+
mark = h.cs([]byte("@"), fgCyan)
327+
val = h.cs([]byte(t.String()), fgCyan)
328328
break
329329
}
330330

331331
if d, ok := av.(*time.Duration); ok {
332-
m = h.cs([]byte("@"), fgCyan)
333-
v = h.cs([]byte(d.String()), fgCyan)
332+
mark = h.cs([]byte("@"), fgCyan)
333+
val = h.cs([]byte(d.String()), fgCyan)
334334
break
335335
}
336336

337337
if textMarshaller, ok := av.(encoding.TextMarshaler); ok {
338-
v = atb(textMarshaller)
338+
val = atb(textMarshaller)
339339
break
340340
}
341341

342342
if h.opts.StringerFormatter {
343343
if stringer, ok := av.(fmt.Stringer); ok {
344-
v = []byte(stringer.String())
344+
val = []byte(stringer.String())
345345
break
346346
}
347347
}
348348

349349
avt := reflect.TypeOf(av)
350350
avv := reflect.ValueOf(av)
351351
if avt == nil {
352-
m = h.cs([]byte("!"), fgRed)
353-
v = h.nilString()
352+
mark = h.cs([]byte("!"), fgRed)
353+
val = h.nilString()
354354
break
355355
}
356356

357357
ut, uv, ptrs := h.reducePointerTypeValue(avt, avv)
358-
v = bytes.Repeat(h.cs([]byte("*"), fgRed), ptrs)
358+
val = bytes.Repeat(h.cs([]byte("*"), fgRed), ptrs)
359359

360360
switch ut.Kind() {
361361
case reflect.Array:
362-
m = h.cs([]byte("A"), fgGreen)
363-
v = h.formatSlice(avt, avv, l, vi)
362+
mark = h.cs([]byte("A"), fgGreen)
363+
val = h.formatSlice(avt, avv, l, vi)
364364
case reflect.Slice:
365-
m = h.cs([]byte("S"), fgGreen)
366-
v = h.formatSlice(avt, avv, l, vi)
365+
mark = h.cs([]byte("S"), fgGreen)
366+
val = h.formatSlice(avt, avv, l, vi)
367367
case reflect.Map:
368-
m = h.cs([]byte("M"), fgGreen)
369-
v = h.formatMap(avt, avv, l, vi)
368+
mark = h.cs([]byte("M"), fgGreen)
369+
val = h.formatMap(avt, avv, l, vi)
370370
case reflect.Struct:
371-
m = h.cs([]byte("S"), fgYellow)
372-
v = h.formatStruct(avt, avv, 0, vi)
371+
mark = h.cs([]byte("S"), fgYellow)
372+
val = h.formatStruct(avt, avv, 0, vi)
373373
case reflect.Float32, reflect.Float64:
374-
m = h.cs([]byte("#"), fgYellow)
374+
mark = h.cs([]byte("#"), fgYellow)
375375
vs = atb(uv.Float())
376-
v = append(v, h.cs(vs, fgYellow)...)
376+
val = append(val, h.cs(vs, fgYellow)...)
377377
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
378-
m = h.cs([]byte("#"), fgYellow)
378+
mark = h.cs([]byte("#"), fgYellow)
379379
vs = atb(uv.Int())
380-
v = append(v, h.cs(vs, fgYellow)...)
380+
val = append(val, h.cs(vs, fgYellow)...)
381381
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
382-
m = h.cs([]byte("#"), fgYellow)
382+
mark = h.cs([]byte("#"), fgYellow)
383383
vs = atb(uv.Uint())
384-
v = append(v, h.cs(vs, fgYellow)...)
384+
val = append(val, h.cs(vs, fgYellow)...)
385385
case reflect.Bool:
386-
m = h.cs([]byte("#"), fgRed)
386+
mark = h.cs([]byte("#"), fgRed)
387387
vs = atb(uv.Bool())
388-
v = append(v, h.cs(vs, fgRed)...)
388+
val = append(val, h.cs(vs, fgRed)...)
389389
case reflect.String:
390390
s := uv.String()
391391
if len(s) == 0 {
392-
v = h.csf([]byte("empty"), fgWhite)
392+
val = h.csf([]byte("empty"), fgWhite)
393393
} else if h.isURL([]byte(s)) {
394-
v = h.ul(h.cs(v, fgBlue))
394+
val = h.ul(h.cs(val, fgBlue))
395395
} else {
396-
v = []byte(uv.String())
396+
val = []byte(uv.String())
397397
}
398398
default:
399-
m = h.cs([]byte("!"), fgRed)
400-
v = h.cs(atb("Unknown type"), fgRed)
399+
mark = h.cs([]byte("!"), fgRed)
400+
val = h.cs(atb("Unknown type"), fgRed)
401401
}
402402
case slog.KindGroup:
403-
m = h.cs([]byte("G"), fgGreen)
403+
mark = h.cs([]byte("G"), fgGreen)
404404
var ga attributes
405405
ga = a.Value.Group()
406-
g = append(g, a.Key)
406+
group = append(group, a.Key)
407407

408-
v = []byte("\n")
409-
v = append(v, h.colorize(nil, ga, l+1, g, vi)...)
408+
val = []byte("\n")
409+
val = append(val, h.colorize(nil, ga, l+1, group, vi)...)
410410
}
411411

412412
b = append(b, bytes.Repeat([]byte(" "), l*2)...)
413-
b = append(b, m...)
413+
b = append(b, mark...)
414414
b = append(b, ' ')
415-
b = append(b, k...)
416-
b = append(b, bytes.Repeat([]byte(" "), pc-len(k))...)
415+
b = append(b, key...)
416+
b = append(b, bytes.Repeat([]byte(" "), paddingColor-len(key))...)
417417
b = append(b, ':', ' ')
418-
b = append(b, v...)
418+
b = append(b, val...)
419419

420420
stringer := reflect.ValueOf(a.Value).MethodByName("String")
421-
if stringer.IsValid() && !bytes.Equal(vo, vs) {
421+
if stringer.IsValid() && !bytes.Equal(valOld, vs) {
422422
s := []byte(` "`)
423423
s = append(s, []byte(a.Value.String())...)
424424
s = append(s, '"')
@@ -433,6 +433,26 @@ func (h *developHandler) colorize(b []byte, as attributes, l int, g []string, vi
433433
return b
434434
}
435435

436+
func (h *developHandler) padding(a attributes, g []string, color foregroundColor, colorFunction func(b []byte, fgColor foregroundColor) []byte) int {
437+
var padding int
438+
for _, attr := range a {
439+
if h.opts.ReplaceAttr != nil {
440+
attr = h.opts.ReplaceAttr(g, attr)
441+
}
442+
443+
colorLength := len(attr.Key)
444+
if color != nil {
445+
colorLength = len(colorFunction([]byte(attr.Key), color))
446+
}
447+
448+
if colorLength > padding {
449+
padding = colorLength
450+
}
451+
}
452+
453+
return padding
454+
}
455+
436456
func (h *developHandler) isURL(u []byte) bool {
437457
_, err := url.ParseRequestURI(string(u))
438458
return err == nil

0 commit comments

Comments
 (0)