-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
diff.go
44 lines (39 loc) · 1.03 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
package carapace
import (
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/pkg/style"
)
// Diff compares values of two actions.
// It overrides the style to hightlight changes.
//
// red: only present in original
// dim: present in both
// green: only present in new
func Diff(original, new Action) Action {
return ActionCallback(func(c Context) Action {
invokedBatch := Batch(
original,
new,
).Invoke(c)
merged := make(map[string]common.RawValue)
for _, v := range invokedBatch[0].action.rawValues {
v.Style = style.Red
merged[v.Value] = v
}
for _, v := range invokedBatch[1].action.rawValues {
if _, ok := merged[v.Value]; ok {
v.Style = style.Dim
merged[v.Value] = v
} else {
v.Style = style.Green
merged[v.Value] = v
}
}
mergedBatch := invokedBatch.Merge()
mergedBatch.action.rawValues = make(common.RawValues, 0)
for _, v := range merged {
mergedBatch.action.rawValues = append(mergedBatch.action.rawValues, v)
}
return mergedBatch.ToA()
})
}