promptui
promptui copied to clipboard
how to implement Searcher to support children node search ?
I have an object like this, I want to search node no matter which layer it is
type Node struct {
Name string `yaml:"name"`
Children []*Node `yaml:"children"`
}
because limited by the interface,I dont know how to implement it?
// Searcher is a function that can be implemented to refine the base searching algorithm in selects.
//
// Search is a function that will receive the searched term and the item's index and should return a boolean
// for whether or not the terms are alike. It is unimplemented by default and search will not work unless
// it is implemented.
Searcher list.Searcher
The following code can only implement top level search. but it could support multi level select.
func choose(parent, trees []*sshw.Node) *sshw.Node {
prompt := promptui.Select{
Label: "select host",
Items: trees,
Templates: templates,
Size: 20,
Searcher: func(input string, index int) bool {
node := trees[index]
content := node.Name
if strings.Contains(input, " ") {
for _, key := range strings.Split(input, " ") {
key = strings.TrimSpace(key)
if key != "" {
if !strings.Contains(content, key) {
return false
}
}
}
return true
}
if strings.Contains(content, input) {
return true
}
return false
},
}
index, _, err := prompt.Run()
if err != nil {
return nil
}
node := trees[index]
if len(node.Children) > 0 {
first := node.Children[0]
if first.Name != prev {
first = &sshw.Node{Name: prev}
node.Children = append(node.Children[:0], append([]*sshw.Node{first}, node.Children...)...)
}
return choose(trees, node.Children)
}
if node.Name == prev {
return choose(nil, parent)
}
return node
}
just analyze the source code, feature search
don't support rebuild list.So it maybe not support tree node search, right?
func (l *List) search(term string) {
var scope []*interface{}
for i, item := range l.items {
if l.Searcher(term, i) {
scope = append(scope, item)
}
}
l.scope = scope
}