-
Notifications
You must be signed in to change notification settings - Fork 1
/
advisor.go
161 lines (131 loc) · 3.51 KB
/
advisor.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package advisor
import (
"fmt"
"github.com/c-bata/go-prompt"
"github.com/sunist-c/genius-invokation-simulator-cli/localization"
)
type Advisor interface {
Entrance() string
NodeDepth() int
SyncDocument(document *prompt.Document)
Suggestions() []prompt.Suggest
ParseEntrance(nextEntrance string) Advisor
PluginChildWithOpts(childOptions ...Options) (child Advisor)
Handler() func(argument string)
}
type implement struct {
suggesters []SuggesterFunc
entrance string
depth int
nodes map[string]Advisor
handler func(argument string)
document *prompt.Document
}
func (impl *implement) Entrance() string {
return impl.entrance
}
func (impl *implement) NodeDepth() int {
return impl.depth
}
func (impl *implement) SyncDocument(document *prompt.Document) {
impl.document = document
}
func (impl *implement) Suggestions() []prompt.Suggest {
if impl.suggesters == nil {
impl.suggesters = []SuggesterFunc{}
}
ctx := NewContext(impl.document, impl.suggesters...)
ctx.Next()
return ctx.Result()
}
func (impl *implement) ParseEntrance(nextEntrance string) Advisor {
if impl.nodes == nil {
impl.nodes = map[string]Advisor{}
}
return impl.nodes[nextEntrance]
}
func (impl *implement) PluginChildWithOpts(childOptions ...Options) (child Advisor) {
if impl.nodes == nil {
impl.nodes = map[string]Advisor{}
}
childEntity := &implement{
entrance: "",
depth: impl.depth + 1,
nodes: map[string]Advisor{},
}
for _, option := range childOptions {
option(childEntity)
}
impl.nodes[childEntity.Entrance()] = childEntity
return childEntity
}
func (impl *implement) Handler() func(argument string) {
if impl.handler == nil {
impl.handler = func(argument string) {}
}
return impl.handler
}
type Options func(option *implement)
func WithAdvisorDepth(depth int) Options {
return func(option *implement) {
option.depth = depth
}
}
func WithAdvisorEntrance(entrance string) Options {
return func(option *implement) {
option.entrance = entrance
}
}
func WithAdvisorBuiltinSuggestions(keywords ...string) Options {
return func(option *implement) {
if option.suggesters == nil {
option.suggesters = []SuggesterFunc{}
}
option.suggesters = append(option.suggesters, func(ctx *SuggesterContext) {
ctx.Next()
appended := make([]prompt.Suggest, len(keywords))
for i := range keywords {
appended[i] = prompt.Suggest{
Text: keywords[i],
Description: localization.LanguagePack.GetTranslation(localization.GetLocalLanguage(), fmt.Sprintf("%v_desc", keywords[i])),
}
}
ctx.AppendSuggestAfter(prompt.FilterHasPrefix(appended, ctx.Document.GetWordBeforeCursor(), true)...)
})
}
}
func WithAdvisorSuggesterFunctions(functions ...SuggesterFunc) Options {
return func(option *implement) {
option.suggesters = functions
}
}
func WithAdvisorSuggesterAppend(functions ...SuggesterFunc) Options {
return func(option *implement) {
option.suggesters = append(option.suggesters, functions...)
}
}
func WithAdvisorFunctionChain(functions ...func(argument string)) Options {
return func(option *implement) {
option.handler = func(argument string) {
for _, function := range functions {
function(argument)
}
}
}
}
func WithAdvisorChildren(nodes ...Advisor) Options {
return func(option *implement) {
for _, node := range nodes {
option.nodes[node.Entrance()] = node
}
}
}
func NewAdvisorWithOpts(options ...Options) Advisor {
impl := &implement{
nodes: map[string]Advisor{},
}
for _, option := range options {
option(impl)
}
return impl
}