forked from abiosoft/ishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completer.go
59 lines (52 loc) · 1.19 KB
/
completer.go
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
package ishell
import (
"strings"
"github.com/flynn-archive/go-shlex"
)
type iCompleter struct {
cmd *Cmd
disabled func() bool
}
func (ic iCompleter) Do(line []rune, pos int) (newLine [][]rune, length int) {
if ic.disabled != nil && ic.disabled() {
return nil, len(line)
}
var words []string
if w, err := shlex.Split(string(line)); err == nil {
words = w
} else {
// fall back
words = strings.Fields(string(line))
}
var cWords []string
prefix := ""
if len(words) > 0 && line[pos-1] != ' ' {
prefix = words[len(words)-1]
cWords = ic.getWords(words[:len(words)-1])
} else {
cWords = ic.getWords(words)
}
var suggestions [][]rune
for _, w := range cWords {
if strings.HasPrefix(w, prefix) {
suggestions = append(suggestions, []rune(strings.TrimPrefix(w, prefix)))
}
}
if len(suggestions) == 1 && prefix != "" && string(suggestions[0]) == "" {
suggestions = [][]rune{[]rune(" ")}
}
return suggestions, len(prefix)
}
func (ic iCompleter) getWords(w []string) (s []string) {
cmd, args := ic.cmd.FindCmd(w)
if cmd == nil {
cmd, args = ic.cmd, w
}
if cmd.Completer != nil {
return cmd.Completer(args)
}
for k := range cmd.children {
s = append(s, k)
}
return
}