-
Notifications
You must be signed in to change notification settings - Fork 2
/
diff.go
69 lines (63 loc) · 1.51 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
"fmt"
diffPkg "github.com/j-keck/zfs-snap-diff/pkg/diff"
"strings"
)
func diffsPrettyText(diff diffPkg.Diff, colored bool) string {
var buff bytes.Buffer
for n, deltas := range diff.Deltas {
header := fmt.Sprintf("Chunk %d - starting at line %d", n, deltas[0].LineNrFrom)
buff.WriteString(strings.Repeat("=", len(header)) + "\n")
buff.WriteString(header + "\n")
buff.WriteString(strings.Repeat("-", len(header)) + "\n")
buff.WriteString(diffPrettyText(deltas, colored))
buff.WriteString("\n")
}
return buff.String()
}
func diffPrettyText(deltas diffPkg.Deltas, colored bool) string {
var buff bytes.Buffer
for _, delta := range deltas {
switch delta.Type {
case diffPkg.Ins:
if colored {
buff.WriteString("\x1b[32m")
buff.WriteString(delta.Text)
buff.WriteString("\x1b[0m")
} else {
for n, line := range strings.Split(delta.Text, "\n") {
if n > 0 {
buff.WriteString("\n")
}
if len(line) > 0 {
buff.WriteString("+ " + line)
}
}
}
case diffPkg.Del:
if colored {
buff.WriteString("\x1b[31m")
buff.WriteString(delta.Text)
buff.WriteString("\x1b[0m")
} else {
for n, line := range strings.Split(delta.Text, "\n") {
if n > 0 {
buff.WriteString("\n")
}
if len(line) > 0 {
buff.WriteString("- " + line)
}
}
}
case diffPkg.Eq:
if colored {
buff.WriteString(delta.Text)
} else {
buff.WriteString(" " + delta.Text)
}
}
}
return buff.String()
}