Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

where are the non pretty options? #137

Open
ghost opened this issue Mar 4, 2023 · 4 comments
Open

where are the non pretty options? #137

ghost opened this issue Mar 4, 2023 · 4 comments

Comments

@ghost
Copy link

ghost commented Mar 4, 2023

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
Copy link
Author

ghost commented Mar 4, 2023

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
Copy link

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
Copy link
Author

ghost commented May 16, 2023

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()
}

@guettli
Copy link

guettli commented Sep 13, 2024

@sergi I miss that feature, too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants