diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9253c9e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/RageCage64/pretty-diff-reporter + +go 1.18 + +require github.com/google/go-cmp v0.5.9 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..62841cd --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/reporter.go b/reporter.go new file mode 100644 index 0000000..2638542 --- /dev/null +++ b/reporter.go @@ -0,0 +1,54 @@ +package prettyreporter + +import ( + "fmt" + "strings" + + "github.com/google/go-cmp/cmp" +) + +type Reporter struct { + path cmp.Path + old []string + new []string + diffCount int +} + +func (r *Reporter) PushStep(ps cmp.PathStep) { + r.path = append(r.path, ps) +} + +func (r *Reporter) Report(rs cmp.Result) { + vOld, vNew := r.path.Last().Values() + if !rs.Equal() { + r.diffCount++ + if vOld.IsValid() { + r.old = append(r.old, fmt.Sprintf("%+v", vOld)) + } + if vNew.IsValid() { + r.new = append(r.new, fmt.Sprintf("%+v", vNew)) + } + } else { + r.old = append(r.old, "") + r.new = append(r.new, fmt.Sprintf("%+v", vOld)) + } +} + +func (r *Reporter) PopStep() { + r.path = r.path[:len(r.path)-1] +} + +func (r *Reporter) String() string { + + return strings.Join(r.lines, "\n") +} + +func maxLen(strs []string) int { + max := 0 + for _, s := range strs { + if len(s) > max { + max = len(s) + } + } + return max +}