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"
)
func MultilineDiff(a, b, lineSep string) string {
reporter := Reporter{
LineSep: lineSep,
}
// Get the diff between two strings.
func Diff(a, b, lineSep string) (string, int) {
reporter := Reporter{LineSep: lineSep}
cmp.Diff(
a, b,
cmpopts.AcyclicTransformer("multiline", func(s string) []string {
@ -18,5 +17,5 @@ func MultilineDiff(a, b, lineSep string) string {
}),
cmp.Reporter(&reporter),
)
return reporter.String()
return reporter.String(), reporter.DiffCount
}

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

Loading…
Cancel
Save