diff options
| author | Eyal Posener <[email protected]> | 2017-05-11 02:20:27 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-11 02:20:27 +0300 |
| commit | dd2171d085ef5957a1c5c0794d6007822e47849b (patch) | |
| tree | 4881f90157f3605fbb488c32e0faae7dff1bc818 /command.go | |
| parent | 1c743d8c0b8235ea2dbf0856987f8bd5b77a0042 (diff) | |
| parent | 72dfe017e9209c1809cfcfcbd9039551fe4d2103 (diff) | |
Merge pull request #11 from posener/files-complete
Improve files and directories completion
Diffstat (limited to 'command.go')
| -rw-r--r-- | command.go | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -33,11 +33,13 @@ func (c *Command) options(args []string) (options []match.Matcher, only bool) { // remove the first argument, which is the command name args = args[1:] - last := last(args) - // if prev has something that needs to follow it, + wordCurrent := last(args) + wordCompleted := last(removeLast(args)) + // if wordCompleted has something that needs to follow it, // it is the most relevant completion - if predicate, ok := c.Flags[last]; ok && predicate != nil { - return predicate.predict(last), true + if predicate, ok := c.Flags[wordCompleted]; ok && predicate != nil { + Log("Predicting according to flag %s", wordCurrent) + return predicate.predict(wordCurrent), true } sub, options, only := c.searchSub(args) @@ -57,7 +59,7 @@ func (c *Command) options(args []string) (options []match.Matcher, only bool) { } // add additional expected argument of the command - options = append(options, c.Args.predict(last)...) + options = append(options, c.Args.predict(wordCurrent)...) return } @@ -65,7 +67,12 @@ func (c *Command) options(args []string) (options []match.Matcher, only bool) { // searchSub searches recursively within sub commands if the sub command appear // in the on of the arguments. func (c *Command) searchSub(args []string) (sub string, all []match.Matcher, only bool) { - for i, arg := range args { + + // search for sub command in all arguments except the last one + // because that one might not be completed yet + searchArgs := removeLast(args) + + for i, arg := range searchArgs { if cmd, ok := c.Sub[arg]; ok { sub = arg all, only = cmd.options(args[i:]) @@ -83,3 +90,10 @@ func (c *Command) subCommands() []match.Matcher { } return subs } + +func removeLast(a []string) []string { + if len(a) > 0 { + return a[:len(a)-1] + } + return a +} |
