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

Method DiffPrettyText

diffmatchpatch/diff.go:1159–1194  ·  view source on GitHub ↗

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

(diffs []Diff)

Source from the content-addressed store, hash-verified

1157
1158// DiffPrettyText converts a []Diff into a colored text report.
1159func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
1160 var buff bytes.Buffer
1161 for _, diff := range diffs {
1162 text := diff.Text
1163
1164 switch diff.Type {
1165 case DiffInsert:
1166 lines := strings.Split(text, "\n")
1167 for i, line := range lines {
1168 _, _ = buff.WriteString("\x1b[32m")
1169 _, _ = buff.WriteString(line)
1170 if i < len(lines)-1 {
1171 _, _ = buff.WriteString("\x1b[0m\n")
1172 } else {
1173 _, _ = buff.WriteString("\x1b[0m")
1174 }
1175 }
1176
1177 case DiffDelete:
1178 lines := strings.Split(text, "\n")
1179 for i, line := range lines {
1180 _, _ = buff.WriteString("\x1b[31m")
1181 _, _ = buff.WriteString(line)
1182 if i < len(lines)-1 {
1183 _, _ = buff.WriteString("\x1b[0m\n")
1184 } else {
1185 _, _ = buff.WriteString("\x1b[0m")
1186 }
1187 }
1188 case DiffEqual:
1189 _, _ = buff.WriteString(text)
1190 }
1191 }
1192
1193 return buff.String()
1194}
1195
1196// DiffText1 computes and returns the source text (all equalities and deletions).
1197func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {

Callers 1

TestDiffPrettyTextFunction · 0.80

Calls 1

StringMethod · 0.45

Tested by 1

TestDiffPrettyTextFunction · 0.64