diff --git a/gocomplete/complete.go b/gocomplete/complete.go index 11a948f..ca87f14 100644 --- a/gocomplete/complete.go +++ b/gocomplete/complete.go @@ -186,5 +186,5 @@ func main() { }, } - complete.New(gogo).Complete() + complete.Run(gogo) } diff --git a/complete.go b/run.go similarity index 58% rename from complete.go rename to run.go index 302486c..d0c9a57 100644 --- a/complete.go +++ b/run.go @@ -11,33 +11,28 @@ const ( envDebug = "COMP_DEBUG" ) -type Completer struct { - Command -} - -func New(c Command) *Completer { - return &Completer{Command: c} -} - -func (c *Completer) Complete() { +// Run get a command, get the typed arguments from environment +// variable, and print out the complete options +func Run(c Command) { args := getLine() Log("Completing args: %s", args) - options := c.complete(args) + options := complete(c, args) Log("Completion: %s", options) output(options) } -func (c *Completer) complete(args []string) []string { - all, _ := c.options(args[:len(args)-1]) - return c.chooseRelevant(last(args), all) -} +// complete get a command an command line arguments and returns +// matching completion options +func complete(c Command, args []string) (matching []string) { + options, _ := c.options(args[:len(args)-1]) -func (c *Completer) chooseRelevant(last string, options []Option) (relevant []string) { + // choose only matching options + l := last(args) for _, option := range options { - if option.Matches(last) { - relevant = append(relevant, option.String()) + if option.Matches(l) { + matching = append(matching, option.String()) } } return diff --git a/complete_test.go b/run_test.go similarity index 84% rename from complete_test.go rename to run_test.go index 6bee548..8f47431 100644 --- a/complete_test.go +++ b/run_test.go @@ -13,29 +13,27 @@ func TestCompleter_Complete(t *testing.T) { os.Setenv(envDebug, "1") } - c := Completer{ - Command: Command{ - Sub: map[string]Command{ - "sub1": { - Flags: map[string]Predicate{ - "-flag1": PredictAnything, - "-flag2": PredictNothing, - }, - }, - "sub2": { - Flags: map[string]Predicate{ - "-flag2": PredictNothing, - "-flag3": PredictSet("opt1", "opt2", "opt12"), - }, - Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")), + c := Command{ + Sub: map[string]Command{ + "sub1": { + Flags: map[string]Predicate{ + "-flag1": PredictAnything, + "-flag2": PredictNothing, }, }, - Flags: map[string]Predicate{ - "-h": PredictNothing, - "-global1": PredictAnything, - "-o": PredictFiles("./tests/*.txt"), + "sub2": { + Flags: map[string]Predicate{ + "-flag2": PredictNothing, + "-flag3": PredictSet("opt1", "opt2", "opt12"), + }, + Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")), }, }, + Flags: map[string]Predicate{ + "-h": PredictNothing, + "-global1": PredictAnything, + "-o": PredictFiles("./tests/*.txt"), + }, } allGlobals := []string{} @@ -181,7 +179,7 @@ func TestCompleter_Complete(t *testing.T) { os.Setenv(envComplete, tt.args) args := getLine() - got := c.complete(args) + got := complete(c, args) sort.Strings(tt.want) sort.Strings(got)