-
Notifications
You must be signed in to change notification settings - Fork 263
/
completers.go
45 lines (37 loc) · 1.18 KB
/
completers.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
package metaquery
import (
"strings"
"github.com/c-bata/go-prompt"
)
// CompleterInput is a struct defining input data for the metaquery completer
type CompleterInput struct {
Query string
TableSuggestions []prompt.Suggest
}
type completer func(input *CompleterInput) []prompt.Suggest
// Complete returns completions for metaqueries.
func Complete(input *CompleterInput) []prompt.Suggest {
input.Query = strings.TrimSuffix(input.Query, ";")
cmd, _ := getCmdAndArgs(input.Query)
metaQueryObj, found := metaQueryDefinitions[cmd]
if !found {
return []prompt.Suggest{}
}
if metaQueryObj.completer == nil {
return []prompt.Suggest{}
}
return metaQueryObj.completer(input)
}
func completerFromArgsOf(cmd string) completer {
return func(input *CompleterInput) []prompt.Suggest {
metaQueryDefinition := metaQueryDefinitions[cmd]
suggestions := make([]prompt.Suggest, len(metaQueryDefinition.args))
for idx, arg := range metaQueryDefinition.args {
suggestions[idx] = prompt.Suggest{Text: arg.value, Description: arg.description, Output: arg.value}
}
return suggestions
}
}
func inspectCompleter(input *CompleterInput) []prompt.Suggest {
return input.TableSuggestions
}