diff options
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) } } |
