MCPcopy Create free account
hub / github.com/sergi/go-diff / DiffPrettyText

Method DiffPrettyText

diffmatchpatch/diff.go:1142–1177  ·  view source on GitHub ↗

DiffPrettyText converts a []Diff into a colored text report.

(diffs []Diff)

Source from the content-addressed store, hash-verified

1140
1141// DiffPrettyText converts a []Diff into a colored text report.
1142func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
1143 var buff bytes.Buffer
1144 for _, diff := range diffs {
1145 text := diff.Text
1146
1147 switch diff.Type {
1148 case DiffInsert:
1149 lines := strings.Split(text, "\n")
1150 for i, line := range lines {
1151 _, _ = buff.WriteString("\x1b[32m")
1152 _, _ = buff.WriteString(line)
1153 if i < len(lines)-1 {
1154 _, _ = buff.WriteString("\x1b[0m\n")
1155 } else {
1156 _, _ = buff.WriteString("\x1b[0m")
1157 }
1158 }
1159
1160 case DiffDelete:
1161 lines := strings.Split(text, "\n")
1162 for i, line := range lines {
1163 _, _ = buff.WriteString("\x1b[31m")
1164 _, _ = buff.WriteString(line)
1165 if i < len(lines)-1 {
1166 _, _ = buff.WriteString("\x1b[0m\n")
1167 } else {
1168 _, _ = buff.WriteString("\x1b[0m")
1169 }
1170 }
1171 case DiffEqual:
1172 _, _ = buff.WriteString(text)
1173 }
1174 }
1175
1176 return buff.String()
1177}
1178
1179// DiffText1 computes and returns the source text (all equalities and deletions).
1180func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {

Callers 1

TestDiffPrettyTextFunction · 0.80

Calls 1

StringMethod · 0.45

Tested by 1

TestDiffPrettyTextFunction · 0.64