package mode
import (
)
type Listing interface {
tk.ComboBox
}
type ListingSpec struct {
Bindings tk.Bindings
Caption string
GetItems func(query string) (items []ListingItem, selected int)
Accept func(string) bool
AutoAccept bool
}
type ListingItem struct {
ToAccept string
ToShow ui.Text
}
var errGetItemsMustBeSpecified = errors.New("GetItems must be specified")
func ( cli.App, ListingSpec) (Listing, error) {
if .GetItems == nil {
return nil, errGetItemsMustBeSpecified
}
if .Accept == nil {
.Accept = func(string) bool { return false }
}
if .Caption == "" {
.Caption = " LISTING "
}
:= func( string) {
:= .Accept()
if ! {
.SetAddon(nil, false)
}
}
:= tk.NewComboBox(tk.ComboBoxSpec{
CodeArea: tk.CodeAreaSpec{
Prompt: modePrompt(.Caption, true),
},
ListBox: tk.ListBoxSpec{
Bindings: .Bindings,
OnAccept: func( tk.Items, int) {
(.(listingItems)[].ToAccept)
},
ExtendStyle: true,
},
OnFilter: func( tk.ComboBox, string) {
, := .GetItems()
.ListBox().Reset(listingItems(), )
if .AutoAccept && len() == 1 {
([0].ToAccept)
}
},
})
return , nil
}
type listingItems []ListingItem
func ( listingItems) () int { return len() }
func ( listingItems) ( int) ui.Text { return [].ToShow }