diff options
| author | Eyal Posener <[email protected]> | 2018-10-19 20:10:37 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2018-10-19 20:10:37 +0300 |
| commit | 5fdb1adfd7447867ac25c2dbafbe577ccc6d9e7f (patch) | |
| tree | 1b4936b7ce044bd52e1ae3f9e4cf7c41a2a89a35 /complete.go | |
| parent | 0d98d7ee195f007b3453c002780ed80c76e7c806 (diff) | |
Add support for CMP_POINT
Fixes #72
Diffstat (limited to 'complete.go')
| -rw-r--r-- | complete.go | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/complete.go b/complete.go index 185d1e8..168657e 100644 --- a/complete.go +++ b/complete.go @@ -10,14 +10,16 @@ import ( "fmt" "io" "os" + "strconv" "github.com/posener/complete/cmd" "github.com/posener/complete/match" ) const ( - envComplete = "COMP_LINE" - envDebug = "COMP_DEBUG" + envLine = "COMP_LINE" + envPoint = "COMP_POINT" + envDebug = "COMP_DEBUG" ) // Complete structs define completion for a command with CLI options @@ -55,14 +57,17 @@ func (c *Complete) Run() bool { // For installation: it assumes that flags were added and parsed before // it was called. func (c *Complete) Complete() bool { - line, ok := getLine() + line, point, ok := getEnv() if !ok { // make sure flags parsed, // in case they were not added in the main program return c.CLI.Run() } - Log("Completing line: %s", line) - a := newArgs(line) + + completePhrase := line[:point] + + Log("Completing phrase: %s", completePhrase) + a := newArgs(completePhrase) Log("Completing last field: %s", a.Last) options := c.Command.Predict(a) Log("Options: %s", options) @@ -79,12 +84,19 @@ func (c *Complete) Complete() bool { return true } -func getLine() (string, bool) { - line := os.Getenv(envComplete) +func getEnv() (line string, point int, ok bool) { + line = os.Getenv(envLine) if line == "" { - return "", false + 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) } - return line, true + return line, point, true } func (c *Complete) output(options []string) { |
