@@ -9,17 +9,18 @@ import (
9
9
"github.com/pmezard/go-difflib/difflib"
10
10
11
11
"github.com/mbrt/gmailctl/internal/graph"
12
+ "github.com/mbrt/gmailctl/internal/reporting"
12
13
)
13
14
14
15
// Diff computes the diff between two lists of filters.
15
16
//
16
17
// To compute the diff, IDs are ignored, only the contents of the filters are actually considered.
17
- func Diff (upstream , local Filters , debugInfo bool , contextLines int ) (FiltersDiff , error ) {
18
+ func Diff (upstream , local Filters , debugInfo bool , contextLines int , colorize bool ) (FiltersDiff , error ) {
18
19
// Computing the diff is very expensive, so we have to minimize the number of filters
19
20
// we have to analyze. To do so, we get rid of the filters that are exactly the same,
20
21
// by hashing them.
21
22
added , removed := changedFilters (upstream , local )
22
- return NewMinimalFiltersDiff (added , removed , debugInfo , contextLines ), nil
23
+ return NewMinimalFiltersDiff (added , removed , debugInfo , contextLines , colorize ), nil
23
24
}
24
25
25
26
// NewMinimalFiltersDiff creates a new FiltersDiff with reordered filters, where
@@ -28,11 +29,11 @@ func Diff(upstream, local Filters, debugInfo bool, contextLines int) (FiltersDif
28
29
// The algorithm used is a quadratic approximation to the otherwise NP-complete
29
30
// travel salesman problem. Hopefully the number of filters is low enough to
30
31
// make this not too slow and the approximation not too bad.
31
- func NewMinimalFiltersDiff (added , removed Filters , printDebugInfo bool , contextLines int ) FiltersDiff {
32
+ func NewMinimalFiltersDiff (added , removed Filters , printDebugInfo bool , contextLines int , colorize bool ) FiltersDiff {
32
33
if len (added ) > 0 && len (removed ) > 0 {
33
34
added , removed = reorderWithHungarian (added , removed )
34
35
}
35
- return FiltersDiff {added , removed , printDebugInfo , contextLines }
36
+ return FiltersDiff {added , removed , printDebugInfo , contextLines , colorize }
36
37
}
37
38
38
39
// FiltersDiff contains filters that have been added and removed locally with respect to upstream.
@@ -41,6 +42,7 @@ type FiltersDiff struct {
41
42
Removed Filters
42
43
PrintDebugInfo bool
43
44
ContextLines int
45
+ Colorize bool
44
46
}
45
47
46
48
// Empty returns true if the diff is empty.
@@ -69,6 +71,10 @@ func (f FiltersDiff) String() string {
69
71
// We can't get a diff apparently, let's make something up here
70
72
return fmt .Sprintf ("Removed:\n %s\n Added:\n %s" , removed , added )
71
73
}
74
+ if f .Colorize {
75
+ s = reporting .ColorizeDiff (s )
76
+ }
77
+
72
78
return s
73
79
}
74
80
0 commit comments