Skip to content

Commit b163665

Browse files
committed
Apply changes from sergi#126
1 parent facec63 commit b163665

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

diffmatchpatch/diff.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,15 +1148,32 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
11481148
var buff bytes.Buffer
11491149
for _, diff := range diffs {
11501150
text := diff.Text
1151+
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+
}
11511160

11521161
switch diff.Type {
11531162
case DiffInsert:
1154-
_, _ = buff.WriteString("\x1b[32m")
1155-
_, _ = buff.WriteString(text)
1163+
_, _ = buff.WriteString("\x1b[32m")
1164+
if !containsCharacters {
1165+
text = strings.ReplaceAll(text, " ", "█")
1166+
text = strings.ReplaceAll(text, "\n", "⏎")
1167+
}
1168+
_, _ = buff.WriteString(text)
11561169
_, _ = buff.WriteString("\x1b[0m")
11571170
case DiffDelete:
11581171
_, _ = buff.WriteString("\x1b[31m")
1159-
_, _ = buff.WriteString(text)
1172+
if !containsCharacters {
1173+
text = strings.ReplaceAll(text, " ", "█")
1174+
text = strings.ReplaceAll(text, "\n", "⏎")
1175+
}
1176+
_, _ = buff.WriteString(text)
11601177
_, _ = buff.WriteString("\x1b[0m")
11611178
case DiffEqual:
11621179
_, _ = buff.WriteString(text)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
module github.com/sergi/go-diff
1+
// module github.com/sergi/go-diff
2+
module github.com/truefoundry/go-diff
23

34
require (
45
github.com/davecgh/go-spew v1.1.1 // indirect

0 commit comments

Comments
 (0)