diff options
Diffstat (limited to 'vendor/github.com/posener/complete/complete.go')
-rw-r--r-- | vendor/github.com/posener/complete/complete.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/vendor/github.com/posener/complete/complete.go b/vendor/github.com/posener/complete/complete.go index 185d1e8..725c4de 100644 --- a/vendor/github.com/posener/complete/complete.go +++ b/vendor/github.com/posener/complete/complete.go | |||
@@ -10,14 +10,16 @@ import ( | |||
10 | "fmt" | 10 | "fmt" |
11 | "io" | 11 | "io" |
12 | "os" | 12 | "os" |
13 | "strconv" | ||
13 | 14 | ||
14 | "github.com/posener/complete/cmd" | 15 | "github.com/posener/complete/cmd" |
15 | "github.com/posener/complete/match" | 16 | "github.com/posener/complete/match" |
16 | ) | 17 | ) |
17 | 18 | ||
18 | const ( | 19 | const ( |
19 | envComplete = "COMP_LINE" | 20 | envLine = "COMP_LINE" |
20 | envDebug = "COMP_DEBUG" | 21 | envPoint = "COMP_POINT" |
22 | envDebug = "COMP_DEBUG" | ||
21 | ) | 23 | ) |
22 | 24 | ||
23 | // Complete structs define completion for a command with CLI options | 25 | // Complete structs define completion for a command with CLI options |
@@ -55,13 +57,18 @@ func (c *Complete) Run() bool { | |||
55 | // For installation: it assumes that flags were added and parsed before | 57 | // For installation: it assumes that flags were added and parsed before |
56 | // it was called. | 58 | // it was called. |
57 | func (c *Complete) Complete() bool { | 59 | func (c *Complete) Complete() bool { |
58 | line, ok := getLine() | 60 | line, point, ok := getEnv() |
59 | if !ok { | 61 | if !ok { |
60 | // make sure flags parsed, | 62 | // make sure flags parsed, |
61 | // in case they were not added in the main program | 63 | // in case they were not added in the main program |
62 | return c.CLI.Run() | 64 | return c.CLI.Run() |
63 | } | 65 | } |
64 | Log("Completing line: %s", line) | 66 | |
67 | if point >= 0 && point < len(line) { | ||
68 | line = line[:point] | ||
69 | } | ||
70 | |||
71 | Log("Completing phrase: %s", line) | ||
65 | a := newArgs(line) | 72 | a := newArgs(line) |
66 | Log("Completing last field: %s", a.Last) | 73 | Log("Completing last field: %s", a.Last) |
67 | options := c.Command.Predict(a) | 74 | options := c.Command.Predict(a) |
@@ -79,12 +86,19 @@ func (c *Complete) Complete() bool { | |||
79 | return true | 86 | return true |
80 | } | 87 | } |
81 | 88 | ||
82 | func getLine() (string, bool) { | 89 | func getEnv() (line string, point int, ok bool) { |
83 | line := os.Getenv(envComplete) | 90 | line = os.Getenv(envLine) |
84 | if line == "" { | 91 | if line == "" { |
85 | return "", false | 92 | return |
93 | } | ||
94 | point, err := strconv.Atoi(os.Getenv(envPoint)) | ||
95 | if err != nil { | ||
96 | // If failed parsing point for some reason, set it to point | ||
97 | // on the end of the line. | ||
98 | Log("Failed parsing point %s: %v", os.Getenv(envPoint), err) | ||
99 | point = len(line) | ||
86 | } | 100 | } |
87 | return line, true | 101 | return line, point, true |
88 | } | 102 | } |
89 | 103 | ||
90 | func (c *Complete) output(options []string) { | 104 | func (c *Complete) output(options []string) { |