Skip to content

Commit

Permalink
Print terminating newline even if compact is true
Browse files Browse the repository at this point in the history
Always print a terminating newline in formatterState.format if
terminateWithNewline argument is true, even if compact is true.
  • Loading branch information
Niels Widger committed Jan 21, 2022
1 parent 5370abe commit ae88742
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions jsoncolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ type formatterState struct {
indent string
frames []*frame

printSpace func(string)
printSpace func(s string, force bool)
printComma func()
printColon func()
printObject func(json.Delim)
Expand Down Expand Up @@ -508,8 +508,8 @@ func newFormatterState(f *Formatter, dst io.Writer) *formatterState {
},
}

fs.printSpace = func(s string) {
if fs.compact {
fs.printSpace = func(s string, force bool) {
if fs.compact && !force {
return
}
fmt.Fprint(dst, sprintfSpace(s))
Expand Down Expand Up @@ -601,13 +601,13 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
if x, ok := t.(json.Delim); ok {
if x == json.Delim('{') || x == json.Delim('[') {
if frame.inObject() {
fs.printSpace(" ")
fs.printSpace(" ", false)
} else {
fs.printIndent()
}
err = fs.formatToken(x)
if more {
fs.printSpace("\n")
fs.printSpace("\n", false)
}
frame = fs.enterFrame(x, !more)
} else {
Expand All @@ -621,7 +621,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
fs.printComma()
}
if len(fs.frames) > 1 {
fs.printSpace("\n")
fs.printSpace("\n", false)
}
}
} else {
Expand All @@ -634,7 +634,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
fs.printIndent()
}
if !frame.inField() {
fs.printSpace(" ")
fs.printSpace(" ", false)
}
err = fs.formatToken(t)
if frame.inField() {
Expand All @@ -644,7 +644,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
fs.printComma()
}
if len(fs.frames) > 1 {
fs.printSpace("\n")
fs.printSpace("\n", false)
}
}
}
Expand All @@ -659,7 +659,7 @@ func (fs *formatterState) format(dst io.Writer, src []byte, terminateWithNewline
}

if terminateWithNewline {
fs.printSpace("\n")
fs.printSpace("\n", true)
}

return nil
Expand Down

0 comments on commit ae88742

Please sign in to comment.