Renamings

This commit is contained in:
Eyal Posener 2017-05-05 21:59:10 +03:00
parent 5e07cbd4c2
commit 04d16f6064
5 changed files with 40 additions and 34 deletions

View File

@ -2,7 +2,7 @@ package complete
type Commands map[string]Command type Commands map[string]Command
type Flags map[string]FlagOptions type Flags map[string]Predicate
type Command struct { type Command struct {
Sub Commands 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, // if prev has something that needs to follow it,
// it is the most relevant completion // it is the most relevant completion
if options, ok := c.Flags[last(args)]; ok && options.HasFollow { if predicate, ok := c.Flags[last(args)]; ok && predicate.Expects {
return options.follows(), true return predicate.predict(), true
} }
sub, options, only := c.searchSub(args) sub, options, only := c.searchSub(args)

View File

@ -34,10 +34,10 @@ func (c *Completer) complete(args []string) []string {
return c.chooseRelevant(last(args), all) return c.chooseRelevant(last(args), all)
} }
func (c *Completer) chooseRelevant(last string, list []Option) (options []string) { func (c *Completer) chooseRelevant(last string, options []Option) (relevant []string) {
for _, sub := range list { for _, option := range options {
if sub.Matches(last) { if option.Matches(last) {
options = append(options, sub.String()) relevant = append(relevant, option.String())
} }
} }
return return

View File

@ -17,22 +17,22 @@ func TestCompleter_Complete(t *testing.T) {
Command: Command{ Command: Command{
Sub: map[string]Command{ Sub: map[string]Command{
"sub1": { "sub1": {
Flags: map[string]FlagOptions{ Flags: map[string]Predicate{
"-flag1": FlagUnknownFollow, "-flag1": PredictAnything,
"-flag2": FlagNoFollow, "-flag2": PredictNothing,
}, },
}, },
"sub2": { "sub2": {
Flags: map[string]FlagOptions{ Flags: map[string]Predicate{
"-flag2": FlagNoFollow, "-flag2": PredictNothing,
"-flag3": FlagNoFollow, "-flag3": PredictNothing,
}, },
}, },
}, },
Flags: map[string]FlagOptions{ Flags: map[string]Predicate{
"-h": FlagNoFollow, "-h": PredictNothing,
"-global1": FlagUnknownFollow, "-global1": PredictAnything,
"-o": FlagFileFilter("./gocomplete/*.go"), "-o": PredictFiles("./gocomplete/*.go"),
}, },
}, },
} }

View File

@ -7,15 +7,15 @@ import (
var ( var (
build = complete.Command{ build = complete.Command{
Flags: complete.Flags{ Flags: complete.Flags{
"-o": complete.FlagUnknownFollow, "-o": complete.PredictFiles("*"),
"-i": complete.FlagNoFollow, "-i": complete.PredictNothing,
}, },
} }
test = complete.Command{ test = complete.Command{
Flags: complete.Flags{ Flags: complete.Flags{
"-run": complete.FlagUnknownFollow, "-run": complete.PredictAnything,
"-count": complete.FlagUnknownFollow, "-count": complete.PredictAnything,
}, },
} }
@ -25,7 +25,7 @@ var (
"test": test, "test": test,
}, },
Flags: complete.Flags{ Flags: complete.Flags{
"-h": complete.FlagNoFollow, "-h": complete.PredictNothing,
}, },
} }
) )

View File

@ -5,27 +5,33 @@ import (
"path/filepath" "path/filepath"
) )
type FlagOptions struct { // Predicate determines what terms can follow a command or a flag
HasFollow bool type Predicate struct {
FollowsOptions func() []Option // 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 { func (f *Predicate) predict() []Option {
if f.FollowsOptions == nil { if f.Predictor == nil {
return nil return nil
} }
return f.FollowsOptions() return f.Predictor()
} }
var ( var (
FlagNoFollow = FlagOptions{} PredictNothing = Predicate{Expects: false}
FlagUnknownFollow = FlagOptions{HasFollow: true} PredictAnything = Predicate{Expects: true}
) )
func FlagFileFilter(pattern string) FlagOptions { func PredictFiles(pattern string) Predicate {
return FlagOptions{ return Predicate{
HasFollow: true, Expects: true,
FollowsOptions: glob(pattern), Predictor: glob(pattern),
} }
} }