Skip to content

Commit

Permalink
Add minor change output enhancement
Browse files Browse the repository at this point in the history
Add `sergi/go-diff` package with its string difference functions
to highlight minor string changes (less than x % of characters are
different) better. Currently, the threshold is at 10 %.
  • Loading branch information
HeavyWombat committed May 1, 2018
1 parent 2ecd086 commit 9399fa7
Show file tree
Hide file tree
Showing 14 changed files with 2,341 additions and 23 deletions.
12 changes: 9 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
name = "github.com/pkg/errors"
version = "0.8.0"

[[constraint]]
name = "github.com/sergi/go-diff"
version = "1.0.0"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.2"
Expand Down
32 changes: 28 additions & 4 deletions pkg/dyff/output_human.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/HeavyWombat/dyff/pkg/bunt"
"github.com/HeavyWombat/yaml"
"github.com/sergi/go-diff/diffmatchpatch"
)

// NoTableStyle disables output in table style
Expand Down Expand Up @@ -222,10 +223,33 @@ func writeStringDiff(output *bytes.Buffer, from string, to string) {
createStringWithPrefix(" + ", showWhitespaceCharacters(to), bunt.AdditionGreen))

} else if isMinorChange(from, to) {
// TODO Highlight the actual change more than the common part using https://github.com/sergi/go-diff DiffCommonPrefix and DiffCommonSuffix
output.WriteString(yellow(fmt.Sprintf("%c value change\n", MODIFICATION)))
output.WriteString(createStringWithPrefix(" - ", from, bunt.RemovalRed))
output.WriteString(createStringWithPrefix(" + ", to, bunt.AdditionGreen))
output.WriteString(yellow(fmt.Sprintf("%c minor value change\n", MODIFICATION)))

diffs := diffmatchpatch.New().DiffMain(from, to, false)

output.WriteString(bunt.Colorize(" - ", bunt.RemovalRed))
for _, part := range diffs {
switch part.Type {
case diffmatchpatch.DiffEqual:
output.WriteString(bunt.Colorize(part.Text, bunt.LightSalmon))

case diffmatchpatch.DiffDelete:
output.WriteString(bunt.Colorize(part.Text, bunt.RemovalRed, bunt.Bold))
}
}
output.WriteString("\n")

output.WriteString(bunt.Colorize(" + ", bunt.AdditionGreen))
for _, part := range diffs {
switch part.Type {
case diffmatchpatch.DiffEqual:
output.WriteString(bunt.Colorize(part.Text, bunt.LightGreen))

case diffmatchpatch.DiffInsert:
output.WriteString(bunt.Colorize(part.Text, bunt.AdditionGreen, bunt.Bold))
}
}
output.WriteString("\n")

} else if isMultiLine(from, to) {
output.WriteString(yellow(fmt.Sprintf("%c value change\n", MODIFICATION)))
Expand Down
25 changes: 25 additions & 0 deletions vendor/github.com/sergi/go-diff/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/sergi/go-diff/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/sergi/go-diff/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9399fa7

Please sign in to comment.