generated from openziti/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.go
76 lines (62 loc) · 2.28 KB
/
complete.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package zdecli
import (
"github.com/c-bata/go-prompt"
"github.com/openziti/ziti-db-explorer/zdelib"
"strings"
)
// StateCompleter is used to package a prompt.Completer with location zdelib.State as well as an CommandRegistry.
// This allows a prompt.Completer to provide current bucket location suggestions (i.e. key values completing) as well
// as provide command suggestions.
type StateCompleter struct {
State *zdelib.State
Registry *CommandRegistry
}
// FirstWord returns the first word of the current input or empty string
func (completer *StateCompleter) FirstWord(d prompt.Document) string {
if d.Text == "" {
return ""
}
firstSpace := strings.IndexByte(d.Text, ' ')
if firstSpace == -1 {
return ""
}
return d.Text[0:firstSpace]
}
// Complete implements the signature required of a prompt.Completer. It parses the current prompt.Document to provide
// suggestions and tab-completion for bucket keys as well as Action's from the provided CommandRegistry.
func (completer *StateCompleter) Complete(d prompt.Document) []prompt.Suggest {
var suggestions []prompt.Suggest
firstWord := completer.FirstWord(d)
completer.Registry.GetCommand(firstWord)
found := false
for _, cmd := range completer.Registry.CommandTextToCommand {
if cmd.Matches(firstWord) {
found = true
for _, sug := range cmd.Suggest(completer.State, d) {
suggestions = append(suggestions, sug)
}
break
}
}
if !found && strings.IndexByte(d.Text, ' ') == -1 {
for _, key := range completer.Registry.CommandTexts {
action := completer.Registry.CommandTextToAction[key]
if action.IsSuggested {
suggestions = append(suggestions, action.Suggest)
}
}
}
return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true)
}