Skip to content

Commit 3a8a636

Browse files
committed
diff-patch
1 parent e7fca78 commit 3a8a636

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

diff.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package carapace
22

33
import (
4+
"fmt"
5+
46
"github.com/carapace-sh/carapace/internal/common"
57
"github.com/carapace-sh/carapace/pkg/style"
68
)
@@ -42,3 +44,27 @@ func Diff(original, new Action) Action {
4244
return mergedBatch.ToA()
4345
})
4446
}
47+
48+
// TODO experimental - needs different signature
49+
// TODO return patch format
50+
func DiffPatch(original, new Action, c Context) []string {
51+
diff := Diff(original, new).Invoke(c)
52+
patch := make([]string, 0)
53+
for _, v := range diff.action.rawValues {
54+
s := v.Value
55+
if v.Description != "" {
56+
s += fmt.Sprintf(" (%v)", v.Description)
57+
}
58+
59+
switch v.Style {
60+
case style.Red:
61+
s = "- " + s
62+
case style.Green:
63+
s = "+ " + s
64+
default:
65+
s = " " + s
66+
}
67+
patch = append(patch, s)
68+
}
69+
return patch
70+
}

0 commit comments

Comments
 (0)