sergi / go-diff

Diff, match and patch text in Go
MIT License
1.81k stars 207 forks source link

where are the non pretty options? #137

Open ghost opened 1 year ago

ghost commented 1 year ago

I see these:

https://godocs.io/github.com/sergi/go-diff/diffmatchpatch#DiffMatchPatch.DiffPrettyHtml https://godocs.io/github.com/sergi/go-diff/diffmatchpatch#DiffMatchPatch.DiffPrettyText

how to create a diff without color?

ghost commented 1 year ago

looks like it would just be like this:

func DiffText(diffs []Diff) string {
   var buf strings.Builder
   for _, diff := range diffs {
      buf.WriteString(diff.Text)
   }
   return buf.String()
}
juanique commented 1 year ago

What output do you get from that? I just get the same text as the colored output without color. Which is meaningless because there are no + or - or any symbols to tell which text is added/removed in the diff:

text1 := `abc
x123
def
feg`

text2 := `abc
deg
feg
ooo`

diffs := dmp.DiffMain(text1, text2, false)

With the code above I get this output:

abc
x123
defg
feg
ooo
ghost commented 1 year ago

good catch, looks like this would do it:

func diff_text(diffs []Diff) string {
   var b strings.Builder
   for _, diff := range diffs {
      switch diff.Type {
      case DiffInsert:
         b.WriteByte('+')
         b.WriteString(diff.Text)
      case DiffDelete:
         b.WriteByte('-')
         b.WriteString(diff.Text)
      case DiffEqual:
         b.WriteString(diff.Text)
      }
   }
   return b.String()
}