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 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)

View File

@ -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

View File

@ -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"),
},
},
}

View File

@ -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,
},
}
)

View File

@ -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),
}
}