-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_listener.go
More file actions
47 lines (36 loc) · 921 Bytes
/
key_listener.go
File metadata and controls
47 lines (36 loc) · 921 Bytes
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
package gshell
import (
"strings"
"github.com/chzyer/readline"
)
type KeyListener struct {
shell *Shell
}
func (l *KeyListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
if key == readline.CharTab {
input := string(line)[:pos-1]
l.shell.logger.Info(SHELL_PREFIX, "Tab key pressed: "+input)
parts := strings.Fields(input)
if len(parts) == 0 {
return nil, 0, false
}
cmd := parts[0]
argPrefix := ""
if len(parts) > 1 {
argPrefix = parts[len(parts)-1]
}
var completion string
var found bool
if argPrefix == "" {
completion, found = l.shell.autoCompleteCommand(cmd)
} else {
completion, found = l.shell.autoCompleteArg(cmd, argPrefix)
completion = strings.Join(parts[:len(parts)-1], " ") + " " + completion
}
if found {
return []rune(completion), len(completion), true
}
return nil, 0, false
}
return nil, 0, false
}