package mode
import (
)
type Lastcmd interface {
tk.ComboBox
}
type LastcmdSpec struct {
Bindings tk.Bindings
Store LastcmdStore
Wordifier func(string) []string
}
type LastcmdStore interface {
Cursor(prefix string) histutil.Cursor
}
var _ = LastcmdStore(histutil.Store(nil))
func ( cli.App, LastcmdSpec) (Lastcmd, error) {
if .Store == nil {
return nil, errNoHistoryStore
}
:= .Store.Cursor("")
.Prev()
, := .Get()
if != nil {
return nil, fmt.Errorf("db error: %v", )
}
:= .Wordifier
if == nil {
= strings.Fields
}
:= .Text
:= ()
:= make([]lastcmdEntry, len()+1)
[0] = lastcmdEntry{content: }
for , := range {
[+1] = lastcmdEntry{strconv.Itoa(), strconv.Itoa( - len()), }
}
:= func( string) {
.CodeArea().MutateState(func( *tk.CodeAreaState) {
.Buffer.InsertAtDot()
})
.SetAddon(nil, false)
}
:= tk.NewComboBox(tk.ComboBoxSpec{
CodeArea: tk.CodeAreaSpec{Prompt: modePrompt(" LASTCMD ", true)},
ListBox: tk.ListBoxSpec{
Bindings: .Bindings,
OnAccept: func( tk.Items, int) {
(.(lastcmdItems).entries[].content)
},
},
OnFilter: func( tk.ComboBox, string) {
:= filterLastcmdItems(, )
if len(.entries) == 1 {
(.entries[0].content)
} else {
.ListBox().Reset(, 0)
}
},
})
return , nil
}
type lastcmdItems struct {
negFilter bool
entries []lastcmdEntry
}
type lastcmdEntry struct {
posIndex string
negIndex string
content string
}
func ( []lastcmdEntry, string) lastcmdItems {
if == "" {
return lastcmdItems{false, }
}
var []lastcmdEntry
:= strings.HasPrefix(, "-")
for , := range {
if ( && strings.HasPrefix(.negIndex, )) ||
(! && strings.HasPrefix(.posIndex, )) {
= append(, )
}
}
return lastcmdItems{, }
}
func ( lastcmdItems) ( int) ui.Text {
:= ""
:= .entries[]
if .negFilter {
= .negIndex
} else {
= .posIndex
}
return ui.T(fmt.Sprintf("%3s %s", , .content))
}
func ( lastcmdItems) () int { return len(.entries) }