Skip to content

Commit 0137046

Browse files
committed
Only display whitespace in diff, if the diff is all whitespace
1 parent 127b21c commit 0137046

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

diffmatchpatch/diff.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -1149,17 +1149,30 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
11491149
for _, diff := range diffs {
11501150
text := diff.Text
11511151

1152+
var containsCharacters bool
1153+
// If the diff only consists of whitespace characters, then pretty-print the whiteshace
1154+
for _, char := range text {
1155+
if char != ' ' && char != '\n' {
1156+
containsCharacters = true
1157+
break
1158+
}
1159+
}
1160+
11521161
switch diff.Type {
11531162
case DiffInsert:
11541163
_, _ = buff.WriteString("\x1b[32m")
1155-
text = strings.ReplaceAll(text, " ", "█")
1156-
text = strings.ReplaceAll(text, "\n", "⏎")
1164+
if !containsCharacters {
1165+
text = strings.ReplaceAll(text, " ", "█")
1166+
text = strings.ReplaceAll(text, "\n", "⏎")
1167+
}
11571168
_, _ = buff.WriteString(text)
11581169
_, _ = buff.WriteString("\x1b[0m")
11591170
case DiffDelete:
11601171
_, _ = buff.WriteString("\x1b[31m")
1161-
text = strings.ReplaceAll(text, " ", "█")
1162-
text = strings.ReplaceAll(text, "\n", "⏎")
1172+
if !containsCharacters {
1173+
text = strings.ReplaceAll(text, " ", "█")
1174+
text = strings.ReplaceAll(text, "\n", "⏎")
1175+
}
11631176
_, _ = buff.WriteString(text)
11641177
_, _ = buff.WriteString("\x1b[0m")
11651178
case DiffEqual:

0 commit comments

Comments
 (0)