Skip to content

Commit

Permalink
allow to disable word diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Feb 16, 2022
1 parent d911e2c commit 049c512
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go get go.xrstf.de/stalk
```
Usage of ./stalk:
-c, --context-lines int number of context lines to show in diffs (default 3)
-w, --diff-by-line diff entire lines and do not highlight changes within words
-h, --hide stringArray path expression to hide in output (can be given multiple times)
--hide-managed Do not show managed fields (default true)
-j, --jsonpath string JSON path expression to transform the output (applied before the --show paths)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type options struct {
showPaths []string
selector labels.Selector
showEmpty bool
disableWordDiff bool
contextLines int
verbose bool
}
Expand All @@ -49,6 +50,7 @@ func main() {
namespace: "default",
hideManagedFields: true,
showEmpty: false,
disableWordDiff: false,
contextLines: 3,
}

Expand All @@ -60,6 +62,7 @@ func main() {
pflag.StringArrayVarP(&opt.showPaths, "show", "s", opt.showPaths, "path expression to include in output (can be given multiple times) (applied before the --hide paths)")
pflag.StringArrayVarP(&opt.hidePaths, "hide", "h", opt.hidePaths, "path expression to hide in output (can be given multiple times)")
pflag.BoolVarP(&opt.showEmpty, "show-empty", "e", opt.showEmpty, "do not hide changes which would produce no diff because of --hide/--show/--jsonpath")
pflag.BoolVarP(&opt.disableWordDiff, "diff-by-line", "w", opt.disableWordDiff, "diff entire lines and do not highlight changes within words")
pflag.IntVarP(&opt.contextLines, "context-lines", "c", opt.contextLines, "number of context lines to show in diffs")
pflag.BoolVarP(&opt.verbose, "verbose", "v", opt.verbose, "Enable more verbose output")
pflag.Parse()
Expand All @@ -78,6 +81,7 @@ func main() {
// validate CLI flags
differOpts := &diff.Options{
ContextLines: opt.contextLines,
DisableWordDiff: true,
ExcludePaths: opt.hidePaths,
IncludePaths: opt.showPaths,
HideEmptyDiffs: !opt.showEmpty,
Expand Down
7 changes: 7 additions & 0 deletions pkg/diff/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ func cloneColorTheme(theme map[cdiff.Tag]color.Style) map[cdiff.Tag]color.Style

return result
}

func disableWordDiff(theme map[cdiff.Tag]color.Style) map[cdiff.Tag]color.Style {
theme[cdiff.OpenDeletedModified] = theme[cdiff.OpenDeletedNotModified]
theme[cdiff.OpenInsertedModified] = theme[cdiff.OpenInsertedNotModified]

return theme
}
5 changes: 3 additions & 2 deletions pkg/diff/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

type Options struct {
ContextLines int
HideEmptyDiffs bool
ContextLines int
HideEmptyDiffs bool
DisableWordDiff bool

JSONPath string
compiledJSONPath *jsonpath.JSONPath
Expand Down
8 changes: 7 additions & 1 deletion pkg/diff/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func NewDiffer(opt *Options, log logrus.FieldLogger) (*Differ, error) {
return nil, fmt.Errorf("invalid options: %w", err)
}

if opt.DisableWordDiff {
opt.CreateColorTheme = disableWordDiff(cloneColorTheme(opt.CreateColorTheme))
opt.UpdateColorTheme = disableWordDiff(cloneColorTheme(opt.UpdateColorTheme))
opt.DeleteColorTheme = disableWordDiff(cloneColorTheme(opt.DeleteColorTheme))
}

return &Differ{
opt: opt,
log: log,
Expand Down Expand Up @@ -166,7 +172,7 @@ func diffTitle(obj *unstructured.Unstructured, lastSeen time.Time) string {
timestamp := lastSeen.Format(time.RFC3339)
kind := obj.GroupVersionKind().Kind

return fmt.Sprintf("%s %s v%s (%s) (gen. %d)", kind, objectKey(obj), timestamp, obj.GetResourceVersion(), obj.GetGeneration())
return fmt.Sprintf("%s %s v%s (%s) (gen. %d)", kind, objectKey(obj), obj.GetResourceVersion(), timestamp, obj.GetGeneration())
}

// this ensures that the first line of a context/diff is not placed in the
Expand Down

0 comments on commit 049c512

Please sign in to comment.