rename MultilineDiff, return diff count

main v0.2.0
RageCage64 2 years ago
parent 591ffbbc4d
commit 9edf7d613c

@ -1,22 +0,0 @@
package main
import (
"fmt"
"github.com/RageCage64/multilinediff"
)
func main() {
a := `a
b
c
diff`
b := `a
b
c
f
f
f`
fmt.Println(multilinediff.MultilineDiff(a, b, "\n"))
}

@ -0,0 +1,24 @@
package main
import (
"fmt"
"github.com/RageCage64/multilinediff"
)
func main() {
a := `a
b
c
diff`
b := `a b
c
f
f
f
diff`
diff, count := multilinediff.Diff(a, b, "\n")
fmt.Println(count)
fmt.Println("--------")
fmt.Println(diff)
}

@ -7,10 +7,9 @@ import (
"github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-cmp/cmp/cmpopts"
) )
func MultilineDiff(a, b, lineSep string) string { // Get the diff between two strings.
reporter := Reporter{ func Diff(a, b, lineSep string) (string, int) {
LineSep: lineSep, reporter := Reporter{LineSep: lineSep}
}
cmp.Diff( cmp.Diff(
a, b, a, b,
cmpopts.AcyclicTransformer("multiline", func(s string) []string { cmpopts.AcyclicTransformer("multiline", func(s string) []string {
@ -18,5 +17,5 @@ func MultilineDiff(a, b, lineSep string) string {
}), }),
cmp.Reporter(&reporter), cmp.Reporter(&reporter),
) )
return reporter.String() return reporter.String(), reporter.DiffCount
} }

@ -46,12 +46,13 @@ func (l diffLine) toLine(length int) string {
return line return line
} }
// A pretty reporter to pass into cmp.Diff using the cmd.Reporter function.
type Reporter struct { type Reporter struct {
LineSep string LineSep string
DiffCount int
path cmp.Path path cmp.Path
lines []diffLine lines []diffLine
diffCount int
} }
func (r *Reporter) PushStep(ps cmp.PathStep) { func (r *Reporter) PushStep(ps cmp.PathStep) {
@ -62,7 +63,7 @@ func (r *Reporter) Report(rs cmp.Result) {
line := diffLine{} line := diffLine{}
vOld, vNew := r.path.Last().Values() vOld, vNew := r.path.Last().Values()
if !rs.Equal() { if !rs.Equal() {
r.diffCount++ r.DiffCount++
if vOld.IsValid() { if vOld.IsValid() {
line.diff = diffTypeChange line.diff = diffTypeChange
line.old = fmt.Sprintf("%+v", vOld) line.old = fmt.Sprintf("%+v", vOld)

Loading…
Cancel
Save