File tree 2 files changed +71
-0
lines changed
2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ package carapace
2
+
3
+ import (
4
+ "github.com/rsteube/carapace/internal/common"
5
+ "github.com/rsteube/carapace/pkg/style"
6
+ )
7
+
8
+ // Diff compares values of two actions.
9
+ // It overrides the style to hightlight changes.
10
+ //
11
+ // red: only present in original
12
+ // dim: present in both
13
+ // green: only present in new
14
+ func Diff (original , new Action ) Action {
15
+ return ActionCallback (func (c Context ) Action {
16
+ invokedBatch := Batch (
17
+ original ,
18
+ new ,
19
+ ).Invoke (c )
20
+
21
+ merged := make (map [string ]common.RawValue )
22
+ for _ , v := range invokedBatch [0 ].action .rawValues {
23
+ v .Style = style .Red
24
+ merged [v .Value ] = v
25
+ }
26
+
27
+ for _ , v := range invokedBatch [1 ].action .rawValues {
28
+ if _ , ok := merged [v .Value ]; ok {
29
+ v .Style = style .Dim
30
+ merged [v .Value ] = v
31
+ } else {
32
+ v .Style = style .Green
33
+ merged [v .Value ] = v
34
+ }
35
+ }
36
+
37
+ mergedBatch := invokedBatch .Merge ()
38
+ mergedBatch .action .rawValues = make (common.RawValues , 0 )
39
+ for _ , v := range merged {
40
+ mergedBatch .action .rawValues = append (mergedBatch .action .rawValues , v )
41
+ }
42
+ return mergedBatch .ToA ()
43
+ })
44
+ }
Original file line number Diff line number Diff line change
1
+ package carapace
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/rsteube/carapace/pkg/style"
7
+ )
8
+
9
+ func TestDiff (t * testing.T ) {
10
+ original := ActionValues (
11
+ "removed" ,
12
+ "same" ,
13
+ )
14
+ new := ActionValues (
15
+ "same" ,
16
+ "added" ,
17
+ )
18
+
19
+ assertEqual (t ,
20
+ Diff (original , new ).Invoke (NewContext ()),
21
+ ActionStyledValues (
22
+ "removed" , style .Red ,
23
+ "same" , style .Dim ,
24
+ "added" , style .Green ,
25
+ ).Invoke (NewContext ()),
26
+ )
27
+ }
You can’t perform that action at this time.
0 commit comments