diff options
| author | Eyal Posener <[email protected]> | 2017-11-04 11:52:09 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-11-04 11:52:09 +0200 |
| commit | 00c86494ff7035cfd62f66042e9ca2b118b90122 (patch) | |
| tree | b507c4736c6f8de2e887e76b952961d1c8dcc44c /complete.go | |
| parent | 88e59760adaddb8276c9b15511302890690e2dae (diff) | |
| parent | c45e6fe8516b89faca97fc3a485949a07a9530c7 (diff) | |
Merge pull request #53 from posener/finally-filter-matches
Filter matches as a final stage
Diffstat (limited to 'complete.go')
| -rw-r--r-- | complete.go | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/complete.go b/complete.go index 1df6617..3647c28 100644 --- a/complete.go +++ b/complete.go @@ -8,10 +8,12 @@ package complete import ( "flag" "fmt" + "io" "os" "strings" "github.com/posener/complete/cmd" + "github.com/posener/complete/match" ) const ( @@ -23,6 +25,7 @@ const ( type Complete struct { Command Command cmd.CLI + Out io.Writer } // New creates a new complete command. @@ -34,6 +37,7 @@ func New(name string, command Command) *Complete { return &Complete{ Command: command, CLI: cmd.CLI{Name: name}, + Out: os.Stdout, } } @@ -59,13 +63,19 @@ func (c *Complete) Complete() bool { return c.CLI.Run() } Log("Completing line: %s", line) - a := newArgs(line) - options := c.Command.Predict(a) + Log("Options: %s", options) - Log("Completion: %s", options) - output(options) + // filter only options that match the last argument + matches := []string{} + for _, option := range options { + if match.Prefix(option, a.Last) { + matches = append(matches, option) + } + } + Log("Matches: %s", matches) + c.output(matches) return true } @@ -77,10 +87,9 @@ func getLine() ([]string, bool) { return strings.Split(line, " "), true } -func output(options []string) { - Log("") +func (c *Complete) output(options []string) { // stdout of program defines the complete options for _, option := range options { - fmt.Println(option) + fmt.Fprintln(c.Out, option) } } |
