package mode
import (
)
type Histlist interface {
tk.ComboBox
}
type HistlistSpec struct {
Bindings tk.Bindings
AllCmds func() ([]store.Cmd, error)
Dedup func() bool
Filter FilterSpec
}
func ( cli.App, HistlistSpec) (Histlist, error) {
if .AllCmds == nil {
return nil, errNoHistoryStore
}
if .Dedup == nil {
.Dedup = func() bool { return true }
}
, := .AllCmds()
if != nil {
return nil, fmt.Errorf("db error: %v", .Error())
}
:= map[string]int{}
for , := range {
[.Text] =
}
:= histlistItems{, }
:= tk.NewComboBox(tk.ComboBoxSpec{
CodeArea: tk.CodeAreaSpec{
Prompt: func() ui.Text {
:= " HISTORY "
if .Dedup() {
+= "(dedup on) "
}
return modeLine(, true)
},
Highlighter: .Filter.Highlighter,
},
ListBox: tk.ListBoxSpec{
Bindings: .Bindings,
OnAccept: func( tk.Items, int) {
:= .(histlistItems).entries[].Text
.CodeArea().MutateState(func( *tk.CodeAreaState) {
:= &.Buffer
if .Content == "" {
.InsertAtDot()
} else {
.InsertAtDot("\n" + )
}
})
.SetAddon(nil, false)
},
},
OnFilter: func( tk.ComboBox, string) {
:= .filter(.Filter.makePredicate(), .Dedup())
.ListBox().Reset(, .Len()-1)
},
})
return , nil
}
type histlistItems struct {
entries []store.Cmd
last map[string]int
}
func ( histlistItems) ( func(string) bool, bool) histlistItems {
var []store.Cmd
for , := range .entries {
:= .Text
if && .last[] != {
continue
}
if () {
= append(, )
}
}
return histlistItems{, nil}
}
func ( histlistItems) ( int) ui.Text {
:= .entries[]
return ui.T(fmt.Sprintf("%4d %s", .Seq, .Text))
}
func ( histlistItems) () int { return len(.entries) }