summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-06 08:24:17 +0300
committerEyal Posener <[email protected]>2017-05-06 09:27:51 +0300
commitd33bac720bcaf13a5ee9f6f165293183d2e3e24d (patch)
treef65d42c434e6b67f53431c0c33c30d0c66774abd
parent5dbf53eec0f066e97f443d1d85e1ba9ee288f1b5 (diff)
Remove Complete struct
-rw-r--r--gocomplete/complete.go2
-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)
}
diff --git a/complete.go b/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
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)