forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconstructor.go
316 lines (275 loc) · 8.18 KB
/
constructor.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package prompt
// Option is the type to replace default parameters.
// prompt.New accepts any number of options (this is functional option pattern).
type Option func(prompt *Prompt) error
// Callback function that returns a prompt prefix.
type PrefixCallback func() (prefix string)
const DefaultIndentSize = 2
// WithIndentSize is an option that sets the amount of spaces
// that constitute a single indentation level.
func WithIndentSize(i int) Option {
return func(p *Prompt) error {
p.renderer.indentSize = i
return nil
}
}
// WithLexer set lexer function and enable it.
func WithLexer(lex Lexer) Option {
return func(p *Prompt) error {
p.lexer = lex
return nil
}
}
// WithExecuteOnEnterCallback can be used to set
// a custom callback function that determines whether an Enter key
// should trigger the Executor or add a newline to the user input buffer.
func WithExecuteOnEnterCallback(fn ExecuteOnEnterCallback) Option {
return func(p *Prompt) error {
p.executeOnEnterCallback = fn
return nil
}
}
// WithCompleter is an option that sets a custom Completer object.
func WithCompleter(c Completer) Option {
return func(p *Prompt) error {
p.completion.completer = c
return nil
}
}
// WithReader can be used to set a custom Reader object.
func WithReader(r Reader) Option {
return func(p *Prompt) error {
p.reader = r
return nil
}
}
// WithWriter can be used to set a custom Writer object.
func WithWriter(w Writer) Option {
return func(p *Prompt) error {
registerWriter(w)
p.renderer.out = w
return nil
}
}
// WithTitle can be used to set the title displayed at the header bar of the terminal.
func WithTitle(t string) Option {
return func(p *Prompt) error {
p.renderer.title = t
return nil
}
}
// WithPrefix can be used to set a prefix string for the prompt.
func WithPrefix(prefix string) Option {
return func(p *Prompt) error {
p.renderer.prefixCallback = func() string { return prefix }
return nil
}
}
// WithInitialText can be used to set the initial buffer text.
func WithInitialText(text string) Option {
return func(p *Prompt) error {
p.buffer.InsertTextMoveCursor(text, p.renderer.col, int(p.renderer.row), true)
return nil
}
}
// WithCompletionWordSeparator can be used to set word separators. Enable only ' ' if empty.
func WithCompletionWordSeparator(sep string) Option {
return func(p *Prompt) error {
p.completion.wordSeparator = sep
return nil
}
}
// WithPrefixCallback can be used to change the prefix dynamically by a callback function.
func WithPrefixCallback(f PrefixCallback) Option {
return func(p *Prompt) error {
p.renderer.prefixCallback = f
return nil
}
}
// WithPrefixTextColor change a text color of prefix string
func WithPrefixTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.prefixTextColor = x
return nil
}
}
// WithPrefixBackgroundColor to change a background color of prefix string
func WithPrefixBackgroundColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.prefixBGColor = x
return nil
}
}
// WithInputTextColor to change a color of text which is input by user
func WithInputTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.inputTextColor = x
return nil
}
}
// WithInputBGColor to change a color of background which is input by user
func WithInputBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.inputBGColor = x
return nil
}
}
// WithSuggestionTextColor to change a text color in drop down suggestions.
func WithSuggestionTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.suggestionTextColor = x
return nil
}
}
// WithSuggestionBGColor change a background color in drop down suggestions.
func WithSuggestionBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.suggestionBGColor = x
return nil
}
}
// WithSelectedSuggestionTextColor to change a text color for completed text which is selected inside suggestions drop down box.
func WithSelectedSuggestionTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.selectedSuggestionTextColor = x
return nil
}
}
// WithSelectedSuggestionBGColor to change a background color for completed text which is selected inside suggestions drop down box.
func WithSelectedSuggestionBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.selectedSuggestionBGColor = x
return nil
}
}
// WithDescriptionTextColor to change a background color of description text in drop down suggestions.
func WithDescriptionTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.descriptionTextColor = x
return nil
}
}
// WithDescriptionBGColor to change a background color of description text in drop down suggestions.
func WithDescriptionBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.descriptionBGColor = x
return nil
}
}
// WithSelectedDescriptionTextColor to change a text color of description which is selected inside suggestions drop down box.
func WithSelectedDescriptionTextColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.selectedDescriptionTextColor = x
return nil
}
}
// WithSelectedDescriptionBGColor to change a background color of description which is selected inside suggestions drop down box.
func WithSelectedDescriptionBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.selectedDescriptionBGColor = x
return nil
}
}
// WithScrollbarThumbColor to change a thumb color on scrollbar.
func WithScrollbarThumbColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.scrollbarThumbColor = x
return nil
}
}
// WithScrollbarBGColor to change a background color of scrollbar.
func WithScrollbarBGColor(x Color) Option {
return func(p *Prompt) error {
p.renderer.scrollbarBGColor = x
return nil
}
}
// WithMaxSuggestion specify the max number of displayed suggestions.
func WithMaxSuggestion(x uint16) Option {
return func(p *Prompt) error {
p.completion.max = x
return nil
}
}
// WithHistory to set history expressed by string array.
func WithHistory(x []string) Option {
return func(p *Prompt) error {
p.history.histories = x
p.history.Clear()
return nil
}
}
// WithKeyBindMode set a key bind mode.
func WithKeyBindMode(m KeyBindMode) Option {
return func(p *Prompt) error {
p.keyBindMode = m
return nil
}
}
// WithCompletionOnDown allows for Down arrow key to trigger completion.
func WithCompletionOnDown() Option {
return func(p *Prompt) error {
p.completionOnDown = true
return nil
}
}
// WithKeyBind to set a custom key bind.
func WithKeyBind(b ...KeyBind) Option {
return func(p *Prompt) error {
p.keyBindings = append(p.keyBindings, b...)
return nil
}
}
// WithASCIICodeBind to set a custom key bind.
func WithASCIICodeBind(b ...ASCIICodeBind) Option {
return func(p *Prompt) error {
p.ASCIICodeBindings = append(p.ASCIICodeBindings, b...)
return nil
}
}
// WithShowCompletionAtStart to set completion window is open at start.
func WithShowCompletionAtStart() Option {
return func(p *Prompt) error {
p.completion.showAtStart = true
return nil
}
}
// WithBreakLineCallback to run a callback at every break line
func WithBreakLineCallback(fn func(*Document)) Option {
return func(p *Prompt) error {
p.renderer.breakLineCallback = fn
return nil
}
}
// WithExitChecker set an exit function which checks if go-prompt exits its Run loop
func WithExitChecker(fn ExitChecker) Option {
return func(p *Prompt) error {
p.exitChecker = fn
return nil
}
}
func DefaultExecuteOnEnterCallback(p *Prompt, indentSize int) (int, bool) {
return 0, true
}
func DefaultPrefixCallback() string {
return "> "
}
// New returns a Prompt with powerful auto-completion.
func New(executor Executor, opts ...Option) *Prompt {
pt := &Prompt{
reader: NewStdinReader(),
renderer: NewRenderer(),
buffer: NewBuffer(),
executor: executor,
history: NewHistory(),
completion: NewCompletionManager(6),
executeOnEnterCallback: DefaultExecuteOnEnterCallback,
keyBindMode: EmacsKeyBind, // All the above assume that bash is running in the default Emacs setting
}
for _, opt := range opts {
if err := opt(pt); err != nil {
panic(err)
}
}
return pt
}