-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multilinediff: add multilinediff package
Just copy and pasting the stuff from the external multilinediff package to make it easier to deal with dependency breakages when they happen. Signed-off-by: braydonk <braydonk@google.com>
- Loading branch information
Showing
4 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
github.com/RageCage64/go-assert v0.2.2 h1:wwPA2yibB7XmaQKpw4xk7NfVPdJ1v79GlBvmS8P9ROM= | ||
github.com/RageCage64/go-assert v0.2.2/go.mod h1:YuWzhAJlE4Z2TW2D4msNx1mNU0wA+6lmAL0lP1l7yd4= | ||
github.com/RageCage64/multilinediff v0.2.0 h1:yNSpSF5NXIrmo6bRIgO4Q0g7SXqFD4j/WEcBE+BdCFY= | ||
github.com/RageCage64/multilinediff v0.2.0/go.mod h1:pKr+KLgP0gvRzA+yv0/IUaYQuBYN1ucWysvsL58aMP0= | ||
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc= | ||
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= | ||
github.com/braydonk/yaml v0.7.0 h1:ySkqO7r0MGoCNhiRJqE0Xe9yhINMyvOAB3nFjgyJn2k= | ||
github.com/braydonk/yaml v0.7.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= | ||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= | ||
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package multilinediff | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
// 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 { | ||
return strings.Split(s, lineSep) | ||
}), | ||
cmp.Reporter(&reporter), | ||
) | ||
return reporter.String(), reporter.DiffCount | ||
} | ||
|
||
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 | ||
} | ||
|
||
// A pretty reporter to pass into cmp.Diff using the cmd.Reporter function. | ||
type Reporter struct { | ||
LineSep string | ||
DiffCount int | ||
|
||
path cmp.Path | ||
lines []diffLine | ||
} | ||
|
||
func (r *Reporter) PushStep(ps cmp.PathStep) { | ||
r.path = append(r.path, ps) | ||
} | ||
|
||
func (r *Reporter) Report(rs cmp.Result) { | ||
line := diffLine{} | ||
vOld, vNew := r.path.Last().Values() | ||
if !rs.Equal() { | ||
r.DiffCount++ | ||
if vOld.IsValid() { | ||
line.diff = diffTypeChange | ||
line.old = fmt.Sprintf("%+v", vOld) | ||
} | ||
if vNew.IsValid() { | ||
if line.diff == diffTypeEqual { | ||
line.diff = diffTypeAdd | ||
} | ||
line.new = fmt.Sprintf("%+v", vNew) | ||
} | ||
} else { | ||
line.old = fmt.Sprintf("%+v", vOld) | ||
line.new = fmt.Sprintf("%+v", vOld) | ||
} | ||
r.lines = append(r.lines, line) | ||
} | ||
|
||
func (r *Reporter) PopStep() { | ||
r.path = r.path[:len(r.path)-1] | ||
} | ||
|
||
func (r *Reporter) String() string { | ||
maxLen := 0 | ||
for _, l := range r.lines { | ||
if len(l.old) > maxLen { | ||
maxLen = len(l.old) | ||
} | ||
} | ||
|
||
diffLines := []string{} | ||
for _, l := range r.lines { | ||
diffLines = append(diffLines, l.toLine(maxLen)) | ||
} | ||
|
||
return strings.Join(diffLines, r.LineSep) | ||
} |