got the reporter doing the right thing without colour

main
RageCage64 2 years ago
parent c45e5cc948
commit f80ca76952

@ -1,2 +1,2 @@
# pretty-diff-reporter # multilinediff
Pretty reporter for cmp.Diff with colourized output support Pretty multiline diff tool and library with a custom reporter for cmp.Diff.

@ -0,0 +1,22 @@
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,7 @@
package main
import "fmt"
func main() {
fmt.Println("not ready yet")
}

@ -1,4 +1,4 @@
module github.com/RageCage64/pretty-diff-reporter module github.com/RageCage64/multilinediff
go 1.18 go 1.18

@ -0,0 +1,22 @@
package multilinediff
import (
"strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func MultilineDiff(a, b, lineSep string) string {
reporter := Reporter{
LineSep: lineSep,
}
cmp.Diff(
a, b,
cmpopts.AcyclicTransformer("multiline", func(s string) []string {
return strings.Split(s, lineSep)
}),
cmp.Reporter(&reporter),
)
return reporter.String()
}

@ -1,4 +1,4 @@
package prettyreporter package multilinediff
import ( import (
"fmt" "fmt"
@ -7,10 +7,50 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
) )
type diffType int
const (
diffTypeEqual diffType = iota
diffTypeChange
diffTypeAdd
)
type diffLine struct {
diff diffType
old string
new string
}
func (l diffLine) toLine(length int) string {
line := ""
switch l.diff {
case diffTypeChange:
line += "- "
case diffTypeAdd:
line += "+ "
default:
line += " "
}
line += l.old
for i := 0; i < length-len(l.old); i++ {
line += " "
}
line += " "
line += l.new
return line
}
type Reporter struct { type Reporter struct {
LineSep string
path cmp.Path path cmp.Path
old []string lines []diffLine
new []string
diffCount int diffCount int
} }
@ -19,19 +59,25 @@ func (r *Reporter) PushStep(ps cmp.PathStep) {
} }
func (r *Reporter) Report(rs cmp.Result) { func (r *Reporter) Report(rs cmp.Result) {
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() {
r.old = append(r.old, fmt.Sprintf("%+v", vOld)) line.diff = diffTypeChange
line.old = fmt.Sprintf("%+v", vOld)
} }
if vNew.IsValid() { if vNew.IsValid() {
r.new = append(r.new, fmt.Sprintf("%+v", vNew)) if line.diff == diffTypeEqual {
line.diff = diffTypeAdd
}
line.new = fmt.Sprintf("%+v", vNew)
} }
} else { } else {
r.old = append(r.old, "") line.old = fmt.Sprintf("%+v", vOld)
r.new = append(r.new, fmt.Sprintf("%+v", vOld)) line.new = fmt.Sprintf("%+v", vOld)
} }
r.lines = append(r.lines, line)
} }
func (r *Reporter) PopStep() { func (r *Reporter) PopStep() {
@ -39,16 +85,17 @@ func (r *Reporter) PopStep() {
} }
func (r *Reporter) String() string { func (r *Reporter) String() string {
maxLen := 0
return strings.Join(r.lines, "\n") for _, l := range r.lines {
} if len(l.old) > maxLen {
maxLen = len(l.old)
func maxLen(strs []string) int {
max := 0
for _, s := range strs {
if len(s) > max {
max = len(s)
} }
} }
return max
diffLines := []string{}
for _, l := range r.lines {
diffLines = append(diffLines, l.toLine(maxLen))
}
return strings.Join(diffLines, r.LineSep)
} }

Loading…
Cancel
Save