package carapace
import (
"fmt"
"sort"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/pkg/style"
)
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 ()
})
}
func DiffPatch (original , new Action , c Context ) []string {
diff := Diff (original , new ).Invoke (c )
patch := make ([]string , 0 )
sort .Sort (common .ByValue (diff .action .rawValues ))
for _ , v := range diff .action .rawValues {
s := v .Value
if v .Description != "" {
s += fmt .Sprintf (" (%v)" , v .Description )
}
switch v .Style {
case style .Red :
s = "- " + s
case style .Green :
s = "+ " + s
default :
s = " " + s
}
patch = append (patch , s )
}
return patch
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .