Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions diffmatchpatch/dmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,29 @@ func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
return buff.String()
}

// DiffPrettyText converts a []Diff into a colored text report.
func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text

switch diff.Type {
case DiffInsert:
_, _ = buff.WriteString("\x1b[32m")
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
case DiffDelete:
_, _ = buff.WriteString("\x1b[31m")
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
case DiffEqual:
_, _ = buff.WriteString(text)
}
}

return buff.String()
}

// DiffText1 computes and returns the source text (all equalities and deletions).
func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {
//StringBuilder text = new StringBuilder()
Expand Down
11 changes: 11 additions & 0 deletions diffmatchpatch/dmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,17 @@ func Test_diffPrettyHtml(t *testing.T) {
dmp.DiffPrettyHtml(diffs))
}

func Test_diffPrettyText(t *testing.T) {
dmp := New()
// Pretty print.
diffs := []Diff{
Diff{DiffEqual, "a\n"},
Diff{DiffDelete, "<B>b</B>"},
Diff{DiffInsert, "c&d"}}
assert.Equal(t, "a\n\x1b[31m<B>b</B>\x1b[0m\x1b[32mc&d\x1b[0m",
dmp.DiffPrettyText(diffs))
}

func Test_diffText(t *testing.T) {
dmp := New()
// Compute the source and destination texts.
Expand Down