From 04d16f6064c7c71068f47b4e7106f15a05b6b326 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Fri, 5 May 2017 21:59:10 +0300 Subject: [PATCH] Renamings --- command.go | 6 +++--- complete.go | 8 ++++---- complete_test.go | 20 ++++++++++---------- gocomplete/complete.go | 10 +++++----- flag.go => predicate.go | 30 ++++++++++++++++++------------ 5 files changed, 40 insertions(+), 34 deletions(-) rename flag.go => predicate.go (53%) diff --git a/command.go b/command.go index f3321fb..9292a89 100644 --- a/command.go +++ b/command.go @@ -2,7 +2,7 @@ package complete type Commands map[string]Command -type Flags map[string]FlagOptions +type Flags map[string]Predicate type Command struct { Sub Commands @@ -18,8 +18,8 @@ func (c *Command) options(args []string) (options []Option, only bool) { // if prev has something that needs to follow it, // it is the most relevant completion - if options, ok := c.Flags[last(args)]; ok && options.HasFollow { - return options.follows(), true + if predicate, ok := c.Flags[last(args)]; ok && predicate.Expects { + return predicate.predict(), true } sub, options, only := c.searchSub(args) diff --git a/complete.go b/complete.go index 6345e63..e7e209e 100644 --- a/complete.go +++ b/complete.go @@ -34,10 +34,10 @@ func (c *Completer) complete(args []string) []string { return c.chooseRelevant(last(args), all) } -func (c *Completer) chooseRelevant(last string, list []Option) (options []string) { - for _, sub := range list { - if sub.Matches(last) { - options = append(options, sub.String()) +func (c *Completer) chooseRelevant(last string, options []Option) (relevant []string) { + for _, option := range options { + if option.Matches(last) { + relevant = append(relevant, option.String()) } } return diff --git a/complete_test.go b/complete_test.go index 3485a99..aad747e 100644 --- a/complete_test.go +++ b/complete_test.go @@ -17,22 +17,22 @@ func TestCompleter_Complete(t *testing.T) { Command: Command{ Sub: map[string]Command{ "sub1": { - Flags: map[string]FlagOptions{ - "-flag1": FlagUnknownFollow, - "-flag2": FlagNoFollow, + Flags: map[string]Predicate{ + "-flag1": PredictAnything, + "-flag2": PredictNothing, }, }, "sub2": { - Flags: map[string]FlagOptions{ - "-flag2": FlagNoFollow, - "-flag3": FlagNoFollow, + Flags: map[string]Predicate{ + "-flag2": PredictNothing, + "-flag3": PredictNothing, }, }, }, - Flags: map[string]FlagOptions{ - "-h": FlagNoFollow, - "-global1": FlagUnknownFollow, - "-o": FlagFileFilter("./gocomplete/*.go"), + Flags: map[string]Predicate{ + "-h": PredictNothing, + "-global1": PredictAnything, + "-o": PredictFiles("./gocomplete/*.go"), }, }, } diff --git a/gocomplete/complete.go b/gocomplete/complete.go index 727e190..5b31cc6 100644 --- a/gocomplete/complete.go +++ b/gocomplete/complete.go @@ -7,15 +7,15 @@ import ( var ( build = complete.Command{ Flags: complete.Flags{ - "-o": complete.FlagUnknownFollow, - "-i": complete.FlagNoFollow, + "-o": complete.PredictFiles("*"), + "-i": complete.PredictNothing, }, } test = complete.Command{ Flags: complete.Flags{ - "-run": complete.FlagUnknownFollow, - "-count": complete.FlagUnknownFollow, + "-run": complete.PredictAnything, + "-count": complete.PredictAnything, }, } @@ -25,7 +25,7 @@ var ( "test": test, }, Flags: complete.Flags{ - "-h": complete.FlagNoFollow, + "-h": complete.PredictNothing, }, } ) diff --git a/flag.go b/predicate.go similarity index 53% rename from flag.go rename to predicate.go index 645cb83..5ba544b 100644 --- a/flag.go +++ b/predicate.go @@ -5,27 +5,33 @@ import ( "path/filepath" ) -type FlagOptions struct { - HasFollow bool - FollowsOptions func() []Option +// Predicate determines what terms can follow a command or a flag +type Predicate struct { + // Expects determine if the predicate expects something after. + // flags/commands that do not expect any specific argument should + // leave it on false + Expects bool + // Predictor is function that returns list of arguments that can + // come after the flag/command + Predictor func() []Option } -func (f *FlagOptions) follows() []Option { - if f.FollowsOptions == nil { +func (f *Predicate) predict() []Option { + if f.Predictor == nil { return nil } - return f.FollowsOptions() + return f.Predictor() } var ( - FlagNoFollow = FlagOptions{} - FlagUnknownFollow = FlagOptions{HasFollow: true} + PredictNothing = Predicate{Expects: false} + PredictAnything = Predicate{Expects: true} ) -func FlagFileFilter(pattern string) FlagOptions { - return FlagOptions{ - HasFollow: true, - FollowsOptions: glob(pattern), +func PredictFiles(pattern string) Predicate { + return Predicate{ + Expects: true, + Predictor: glob(pattern), } }