-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.go
More file actions
75 lines (64 loc) · 2.19 KB
/
Copy pathcompletion.go
File metadata and controls
75 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"regexp"
"strings"
"github.com/daichi-m/go-prompt"
)
var splitter *regexp.Regexp = regexp.MustCompile("\\s+")
/*
CmdSuggestions returns the list of suggestions based on the current input.
In case of multi word commands like ACL and LATENCY, it splits the input into two parts
and tries to filter based on the current word that is being input.
E.g., if user enters `AC` suggestions comes up as `ACL LOAD`, `ACL LOG`, `ACL SAVE`, `ACL LIST` etc.
but once user enters `ACL L` suggestions changes to `LOAD`, `LOG` and `LIST` only.
*/
func CmdSuggestions(d prompt.Document) []prompt.Suggest {
cmds := global.supported.completions
full := strings.ToUpper(d.TextBeforeCursor())
curr := strings.ToUpper(d.GetWordBeforeCursor())
parts := splitter.Split(full, -1)
full = strings.Join(parts, " ")
spaced := len(parts) > 1
if len(full) == 0 {
// logger.Debug("Input empty returning empty suggest")
return []prompt.Suggest{}
}
filt := prompt.FilterHasPrefix(cmds, full, true)
if !spaced {
// logger.Debug("Full Input: %s, Current Word: %s, IsSpaced: %t, Filtered Suggestions: %#v\n",
// full, curr, spaced, LogSafeSlice(filt))
return filterComplete(filt, full)
}
modFilt := filterMultiWord(filt, full, curr, parts)
return filterComplete(modFilt, parts[len(parts)-1])
}
func filterMultiWord(filt []prompt.Suggest, full, current string, parts []string) []prompt.Suggest {
spaced := len(parts) > 1
var prefix string
if spaced {
prefix = parts[0]
}
modFilt := make([]prompt.Suggest, 0, len(filt))
for _, x := range filt {
sugg := prompt.Suggest{
Text: strings.TrimSpace(strings.TrimPrefix(x.Text, prefix)),
Description: x.Description,
}
modFilt = append(modFilt, sugg)
}
modFilt = prompt.FilterHasPrefix(modFilt, current, true)
logger.Debug("Full Input: %s, Current Word: %s, Prefix: %s, IsSpaced: %t, Filtered Suggestions: %#v\n",
full, current, prefix, spaced, LogSafeSlice(modFilt))
return modFilt
}
func filterComplete(filt []prompt.Suggest, txt string) []prompt.Suggest {
if len(filt) != 1 {
return filt
}
filt1 := filt[0]
logger.Debug("Suggestions: %#v, Full Text: %s\n", filt, txt)
if filt1.Text == txt {
return []prompt.Suggest{}
}
return filt
}