Skip to content

Commit

Permalink
Only display whitespace in diff, if the diff is all whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
NickyBoy89 committed Feb 12, 2022
1 parent 127b21c commit 0137046
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,17 +1149,30 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string {
for _, diff := range diffs {
text := diff.Text

var containsCharacters bool
// If the diff only consists of whitespace characters, then pretty-print the whiteshace
for _, char := range text {
if char != ' ' && char != '\n' {
containsCharacters = true
break
}
}

switch diff.Type {
case DiffInsert:
_, _ = buff.WriteString("\x1b[32m")
text = strings.ReplaceAll(text, " ", "█")
text = strings.ReplaceAll(text, "\n", "⏎")
if !containsCharacters {
text = strings.ReplaceAll(text, " ", "█")
text = strings.ReplaceAll(text, "\n", "⏎")
}
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
case DiffDelete:
_, _ = buff.WriteString("\x1b[31m")
text = strings.ReplaceAll(text, " ", "█")
text = strings.ReplaceAll(text, "\n", "⏎")
if !containsCharacters {
text = strings.ReplaceAll(text, " ", "█")
text = strings.ReplaceAll(text, "\n", "⏎")
}
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("\x1b[0m")
case DiffEqual:
Expand Down

0 comments on commit 0137046

Please sign in to comment.