Skip to content

Commit

Permalink
Fixed duplicate newline issue
Browse files Browse the repository at this point in the history
The usage of newlines in output details was not implemented in a
common fashion resulting in different numbers of newlines in
different output scenarios. This was fixed by using a common style
and by avoiding newlines in the helper functions. Instead, before
each path is displayed, the first separating newline is produced.
  • Loading branch information
HeavyWombat committed Apr 15, 2018
1 parent e043c1d commit fce7475
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 0 additions & 1 deletion cmd/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ document types are: YAML (http://yaml.org/) and JSON (http://json.org/).
| (_| | |_| | _| _|
\__,_|\__, |_| |_| returned %s
|___/
`, niceLocation(fromLocation),
niceLocation(toLocation),
core.Bold(core.Plural(len(diffs), "difference")))
Expand Down
10 changes: 7 additions & 3 deletions core/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ func DiffsToHumanStyle(diffs []Diff) string {
GenerateHumanDiffOutput(&output, diff)
}

output.WriteString("\n")
return output.String()
}

func GenerateHumanDiffOutput(output *bytes.Buffer, diff Diff) {
output.WriteString("\n")
output.WriteString(pathToString(diff.Path))
output.WriteString("\n")

Expand Down Expand Up @@ -155,7 +157,6 @@ func GenerateHumanDetailOutput(detail Detail) string {
if estimatedLength := max(fromSingleLineLength, toStringleLineLength); estimatedLength < threshold {
output.WriteString(Red(fmt.Sprintf(" - %s\n", strings.Join(from, singleLineSeparator))))
output.WriteString(Green(fmt.Sprintf(" + %s\n", strings.Join(to, singleLineSeparator))))
output.WriteString("\n")

} else {
output.WriteString(Cols(" ", 2,
Expand Down Expand Up @@ -373,9 +374,12 @@ func Cols(separator string, indent int, columns ...string) string {
}

var buf bytes.Buffer
for _, row := range mtrx {
for i, row := range mtrx {
buf.WriteString(strings.TrimRight(strings.Join(row, separator), " "))
buf.WriteString("\n")

if i < len(mtrx)-1 {
buf.WriteString("\n")
}
}

return buf.String()
Expand Down
22 changes: 10 additions & 12 deletions core/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ var _ = Describe("Core/Output", func() {
Context("reporting differences", func() {
It("should show a nice string difference", func() {
content := singleDiff("/some/yaml/structure/string", MODIFICATION, "foobar", "Foobar")
Expect(humanDiff(content)).To(BeEquivalentTo(`some.yaml.structure.string
Expect(humanDiff(content)).To(BeEquivalentTo(`
some.yaml.structure.string
± value change
- foobar
+ Foobar
`))
})

It("should show a nice integer difference", func() {
content := singleDiff("/some/yaml/structure/int", MODIFICATION, 12, 147)
Expect(humanDiff(content)).To(BeEquivalentTo(`some.yaml.structure.int
Expect(humanDiff(content)).To(BeEquivalentTo(`
some.yaml.structure.int
± value change
- 12
+ 147
`))
})

It("should show a type difference", func() {
content := singleDiff("/some/yaml/structure/test", MODIFICATION, 12, 12.0)
Expect(humanDiff(content)).To(BeEquivalentTo(`some.yaml.structure.test
Expect(humanDiff(content)).To(BeEquivalentTo(`
some.yaml.structure.test
± type change from int to float64
- 12
+ 12
`))
})

Expand Down Expand Up @@ -89,11 +89,11 @@ input: |+
"This is a text with\nnewlines and stuff\nto show case whitespace\nissues.\n",
"This is a text with\nnewlines and stuff\nto show case whitespace\nissues.\n\n")))

Expect(humanDiff(result[0])).To(BeEquivalentTo("input\n ± whitespace only change\n - This·is·a·text·with↵ + This·is·a·text·with↵\n" +
Expect(humanDiff(result[0])).To(BeEquivalentTo("\ninput\n ± whitespace only change\n - This·is·a·text·with↵ + This·is·a·text·with↵\n" +
" newlines·and·stuff↵ newlines·and·stuff↵\n" +
" to·show·case·whitespace↵ to·show·case·whitespace↵\n" +
" issues.↵ issues.↵\n" +
" ↵\n\n\n\n"))
" ↵\n\n"))
})
})
})
Expand Down Expand Up @@ -125,8 +125,7 @@ Miss Foobar`
#2 Mrs. Foobar 200
#3 Miss Foobar 3000
#4 40000
500000
`))
500000`))
})

It("should show a nice table output with colored text", func() {
Expand Down Expand Up @@ -159,8 +158,7 @@ Miss Foobar`
%s Mrs. Foobar 200
%s Miss Foobar 3000
%s 40000
500000
`, Color("#1", color.FgGreen), Color("#2", color.FgBlue), Color("#3", color.FgRed, color.Underline), Color("#4", color.FgYellow, color.Bold, color.Italic))))
500000`, Color("#1", color.FgGreen), Color("#2", color.FgBlue), Color("#3", color.FgRed, color.Underline), Color("#4", color.FgYellow, color.Bold, color.Italic))))
})
})
})
Expand Down

0 comments on commit fce7475

Please sign in to comment.