Skip to content

Commit c0674e7

Browse files
committed
Fix indentation
1 parent ea9250b commit c0674e7

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ go 1.13
44

55
require (
66
cloud.google.com/go v0.26.0
7+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
78
github.com/fatih/color v1.15.0
89
github.com/google/go-cmp v0.5.3
10+
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
911
github.com/mattn/go-isatty v0.0.18 // indirect
1012
github.com/nwidger/jsoncolor v0.3.2
1113
go.opencensus.io v0.24.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
22
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
5+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
46
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
57
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
68
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
@@ -36,6 +38,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3638
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
3739
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3840
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
41+
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
42+
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
3943
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
4044
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
4145
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

internal/entryhuman/entry.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/fatih/color"
18-
"github.com/nwidger/jsoncolor"
18+
"github.com/hokaccha/go-prettyjson"
1919
"go.opencensus.io/trace"
2020
"golang.org/x/crypto/ssh/terminal"
2121
"golang.org/x/xerrors"
@@ -116,7 +116,14 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
116116
// No error is guaranteed due to slog.Map handling errors itself.
117117
var fields []byte
118118
if shouldColor(w) {
119-
fields, _ = jsoncolor.MarshalIndent(ent.Fields, "", "")
119+
f := prettyjson.NewFormatter()
120+
f.Newline = ""
121+
f.Indent = 0
122+
fields, _ = f.Marshal(ent.Fields)
123+
// Need a space after ":" to match the non-color output.
124+
var buf bytes.Buffer
125+
_ = json.Indent(&buf, fields, "", "")
126+
fields = buf.Bytes()
120127
} else {
121128
fields, _ = json.MarshalIndent(ent.Fields, "", "")
122129
}

internal/entryhuman/entry_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/acarl005/stripansi"
1011
"go.opencensus.io/trace"
1112

1213
"cdr.dev/slog"
@@ -93,6 +94,25 @@ func TestEntry(t *testing.T) {
9394
})
9495
assert.Equal(t, "entry", "\x1b[0m\x1b[0m0001-01-01 00:00:00.000 \x1b[91m[CRITICAL]\x1b[0m\t\x1b[36m<.:0> \x1b[0m\t\"\"\t{\x1b[34m\"hey\"\x1b[0m: \x1b[32m\"hi\"\x1b[0m}", act)
9596
})
97+
98+
t.Run("colorJustColor", func(t *testing.T) {
99+
t.Parallel()
100+
101+
ent := slog.SinkEntry{
102+
Level: slog.LevelCritical,
103+
Fields: slog.M(
104+
slog.F("hey", "hi"),
105+
),
106+
}
107+
108+
actColor := entryhuman.Fmt(entryhuman.ForceColorWriter, ent)
109+
110+
assert.Equal(t,
111+
"entries",
112+
entryhuman.Fmt(ioutil.Discard, ent),
113+
stripansi.Strip(actColor),
114+
)
115+
})
96116
}
97117

98118
func BenchmarkFmt(b *testing.B) {

0 commit comments

Comments
 (0)