2017-07-18 14:15:54 +00:00
|
|
|
package complete
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-11-07 16:57:05 +00:00
|
|
|
"io"
|
2017-07-18 14:15:54 +00:00
|
|
|
"os"
|
2019-04-12 15:51:37 +00:00
|
|
|
"strconv"
|
2017-07-18 14:15:54 +00:00
|
|
|
|
|
|
|
"github.com/posener/complete/cmd"
|
2017-11-07 16:57:05 +00:00
|
|
|
"github.com/posener/complete/match"
|
2017-07-18 14:15:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-04-12 15:51:37 +00:00
|
|
|
envLine = "COMP_LINE"
|
|
|
|
envPoint = "COMP_POINT"
|
|
|
|
envDebug = "COMP_DEBUG"
|
2017-07-18 14:15:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Complete structs define completion for a command with CLI options
|
|
|
|
type Complete struct {
|
|
|
|
Command Command
|
|
|
|
cmd.CLI
|
2017-11-07 16:57:05 +00:00
|
|
|
Out io.Writer
|
2017-07-18 14:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new complete command.
|
|
|
|
// name is the name of command we want to auto complete.
|
|
|
|
// IMPORTANT: it must be the same name - if the auto complete
|
|
|
|
// completes the 'go' command, name must be equal to "go".
|
|
|
|
// command is the struct of the command completion.
|
|
|
|
func New(name string, command Command) *Complete {
|
|
|
|
return &Complete{
|
|
|
|
Command: command,
|
|
|
|
CLI: cmd.CLI{Name: name},
|
2017-11-07 16:57:05 +00:00
|
|
|
Out: os.Stdout,
|
2017-07-18 14:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the completion and add installation flags beforehand.
|
|
|
|
// The flags are added to the main flag CommandLine variable.
|
|
|
|
func (c *Complete) Run() bool {
|
|
|
|
c.AddFlags(nil)
|
|
|
|
flag.Parse()
|
|
|
|
return c.Complete()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete a command from completion line in environment variable,
|
|
|
|
// and print out the complete options.
|
|
|
|
// returns success if the completion ran or if the cli matched
|
|
|
|
// any of the given flags, false otherwise
|
|
|
|
// For installation: it assumes that flags were added and parsed before
|
|
|
|
// it was called.
|
|
|
|
func (c *Complete) Complete() bool {
|
2019-04-12 15:51:37 +00:00
|
|
|
line, point, ok := getEnv()
|
2017-07-18 14:15:54 +00:00
|
|
|
if !ok {
|
|
|
|
// make sure flags parsed,
|
|
|
|
// in case they were not added in the main program
|
|
|
|
return c.CLI.Run()
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
|
|
|
|
if point >= 0 && point < len(line) {
|
|
|
|
line = line[:point]
|
|
|
|
}
|
|
|
|
|
|
|
|
Log("Completing phrase: %s", line)
|
2017-07-18 14:15:54 +00:00
|
|
|
a := newArgs(line)
|
2017-11-07 16:57:05 +00:00
|
|
|
Log("Completing last field: %s", a.Last)
|
2017-07-18 14:15:54 +00:00
|
|
|
options := c.Command.Predict(a)
|
2017-11-07 16:57:05 +00:00
|
|
|
Log("Options: %s", options)
|
2017-07-18 14:15:54 +00:00
|
|
|
|
2017-11-07 16:57:05 +00:00
|
|
|
// 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)
|
2017-07-18 14:15:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func getEnv() (line string, point int, ok bool) {
|
|
|
|
line = os.Getenv(envLine)
|
2017-07-18 14:15:54 +00:00
|
|
|
if line == "" {
|
2019-04-12 15:51:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
point, err := strconv.Atoi(os.Getenv(envPoint))
|
|
|
|
if err != nil {
|
|
|
|
// If failed parsing point for some reason, set it to point
|
|
|
|
// on the end of the line.
|
|
|
|
Log("Failed parsing point %s: %v", os.Getenv(envPoint), err)
|
|
|
|
point = len(line)
|
2017-07-18 14:15:54 +00:00
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
return line, point, true
|
2017-07-18 14:15:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:57:05 +00:00
|
|
|
func (c *Complete) output(options []string) {
|
2017-07-18 14:15:54 +00:00
|
|
|
// stdout of program defines the complete options
|
|
|
|
for _, option := range options {
|
2017-11-07 16:57:05 +00:00
|
|
|
fmt.Fprintln(c.Out, option)
|
2017-07-18 14:15:54 +00:00
|
|
|
}
|
|
|
|
}
|