diff options
| author | Eyal Posener <[email protected]> | 2017-05-06 08:24:17 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-06 09:27:51 +0300 |
| commit | d33bac720bcaf13a5ee9f6f165293183d2e3e24d (patch) | |
| tree | f65d42c434e6b67f53431c0c33c30d0c66774abd | |
| parent | 5dbf53eec0f066e97f443d1d85e1ba9ee288f1b5 (diff) | |
Remove Complete struct
| -rw-r--r-- | gocomplete/complete.go | 2 | ||||
| -rw-r--r-- | run.go (renamed from complete.go) | 29 | ||||
| -rw-r--r-- | run_test.go (renamed from complete_test.go) | 38 |
3 files changed, 31 insertions, 38 deletions
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) } @@ -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 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) |
