From 127b21c3bc965a2b7bd39a6b81beb9ba6e6d39fb Mon Sep 17 00:00:00 2001 From: Nicholas Novak Date: Thu, 16 Sep 2021 23:38:12 -0700 Subject: [PATCH 1/2] Made diffprettytext replace invisibles with unicode characters --- diffmatchpatch/diff.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/diffmatchpatch/diff.go b/diffmatchpatch/diff.go index 2a9f2dc..318e0e5 100644 --- a/diffmatchpatch/diff.go +++ b/diffmatchpatch/diff.go @@ -1152,10 +1152,14 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string { switch diff.Type { case DiffInsert: _, _ = buff.WriteString("\x1b[32m") + 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", "⏎") _, _ = buff.WriteString(text) _, _ = buff.WriteString("\x1b[0m") case DiffEqual: From 0137046d7acf5966aaaf98f98033f4e30c8f5e11 Mon Sep 17 00:00:00 2001 From: Nicholas Novak Date: Fri, 11 Feb 2022 18:52:28 -0800 Subject: [PATCH 2/2] Only display whitespace in diff, if the diff is all whitespace --- diffmatchpatch/diff.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/diffmatchpatch/diff.go b/diffmatchpatch/diff.go index 318e0e5..37aecae 100644 --- a/diffmatchpatch/diff.go +++ b/diffmatchpatch/diff.go @@ -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: