@@ -22,30 +22,42 @@ func printDiff(want, got string) string {
22
22
switch diff .Type {
23
23
case diffmatchpatch .DiffInsert :
24
24
buff .WriteString ("\x1b [32m" )
25
- buff .WriteString ("+ " )
26
- buff .WriteString (highlightWhitespaces (text , "\x1b [42m" ))
25
+ lines := strings .Split (strings .TrimRight (text , "\n " ), "\n " )
26
+ for _ , line := range lines {
27
+ buff .WriteString ("+ " )
28
+ buff .WriteString (highlightWhitespaces (line , "\x1b [42m" ) + "\n " )
29
+ }
27
30
buff .WriteString ("\x1b [0m" )
28
31
if ! strings .HasSuffix (text , "\n " ) {
29
32
buff .WriteString ("\n \\ No newline at end of file\n " )
30
33
}
31
34
case diffmatchpatch .DiffDelete :
32
35
buff .WriteString ("\x1b [31m" )
33
- buff .WriteString ("- " )
34
- buff .WriteString (highlightWhitespaces (text , "\x1b [41m" ))
36
+ lines := strings .Split (strings .TrimRight (text , "\n " ), "\n " )
37
+ for _ , line := range lines {
38
+ buff .WriteString ("- " )
39
+ buff .WriteString (highlightWhitespaces (line , "\x1b [41m" ) + "\n " )
40
+ }
35
41
buff .WriteString ("\x1b [0m" )
36
42
if ! strings .HasSuffix (text , "\n " ) {
37
43
buff .WriteString ("\n \\ No newline at end of file\n " )
38
44
}
45
+ if ! strings .HasSuffix (text , "\n " ) {
46
+ buff .WriteString ("\n \\ No newline at end of file\n " )
47
+ }
39
48
case diffmatchpatch .DiffEqual :
40
- buff .WriteString (" " )
41
- buff .WriteString (text )
49
+ lines := strings .Split (strings .TrimRight (text , "\n " ), "\n " )
50
+ for _ , line := range lines {
51
+ buff .WriteString (" " )
52
+ buff .WriteString (line + "\n " )
53
+ }
42
54
}
43
55
}
44
56
return buff .String ()
45
57
}
46
58
47
- var tailingWhitespace = regexp .MustCompile (`([ \t]+)\n $` )
59
+ var tailingWhitespace = regexp .MustCompile (`([ \t]+)$` )
48
60
49
61
func highlightWhitespaces (in string , color string ) string {
50
- return tailingWhitespace .ReplaceAllString (in , color + "$1\x1b [49m\n " )
62
+ return tailingWhitespace .ReplaceAllString (in , color + "$1\x1b [49m" )
51
63
}
0 commit comments