Repository navigation
Expand file tree
/
Copy pathstringer.go
More file actions
66 lines (58 loc) · 1.42 KB
/
Copy pathstringer.go
File metadata and controls
66 lines (58 loc) · 1.42 KB
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
package comparator
import "strings"
// String renders a unified diff in the familiar text form: the header followed
// by each hunk's context header and its change lines. Change contents already
// carry their "+", "-", or space prefix.
func (u *UnifiedDiff) String() string {
if u == nil {
return ""
}
var b strings.Builder
b.WriteString(u.Header)
for _, chunk := range u.Chunks {
b.WriteString(chunk.Context)
b.WriteByte('\n')
for _, ch := range chunk.Changes {
b.WriteString(ch.Content)
b.WriteByte('\n')
}
}
return b.String()
}
// String renders a visual diff tree as an indented, one-node-per-line string.
// Each node is prefixed by a status symbol: "+" added, "-" removed, "~"
// different, and a space for unchanged nodes.
func (v *VisualDiff) String() string {
if v == nil || v.Root == nil {
return ""
}
var b strings.Builder
writeVisualNode(&b, v.Root, 0)
return b.String()
}
func writeVisualNode(b *strings.Builder, node *VisualNode, depth int) {
if node == nil {
return
}
symbol := " "
switch node.Status {
case "added":
symbol = "+"
case "removed":
symbol = "-"
case "different":
symbol = "~"
}
b.WriteString(strings.Repeat(" ", depth))
b.WriteString(symbol)
b.WriteByte(' ')
b.WriteString(node.Path)
if node.Value != "" {
b.WriteString(": ")
b.WriteString(node.Value)
}
b.WriteByte('\n')
for _, child := range node.Children {
writeVisualNode(b, child, depth+1)
}
}