Implement searching

This commit is contained in:
Zachary Yedidia
2019-01-03 15:27:43 -05:00
parent f63c72c50d
commit e63a3c8917
10 changed files with 348 additions and 25 deletions

View File

@@ -27,8 +27,10 @@ type InfoBuf struct {
GutterMessage bool
PromptCallback func(resp string, canceled bool)
EventCallback func(resp string)
}
// NewBuffer returns a new infobuffer
func NewBuffer() *InfoBuf {
ib := new(InfoBuf)
ib.History = make(map[string][]string)
@@ -62,7 +64,11 @@ func (i *InfoBuf) Error(msg ...interface{}) {
// TODO: add to log?
}
func (i *InfoBuf) Prompt(prompt string, msg string, callback func(string, bool)) {
// Prompt starts a prompt for the user, it takes a prompt, a possibly partially filled in msg
// and callbacks executed when the user executes an event and when the user finishes the prompt
// The eventcb passes the current user response as the argument and donecb passes the user's message
// and a boolean indicating if the prompt was canceled
func (i *InfoBuf) Prompt(prompt string, msg string, eventcb func(string), donecb func(string, bool)) {
// If we get another prompt mid-prompt we cancel the one getting overwritten
if i.HasPrompt {
i.DonePrompt(true)
@@ -71,21 +77,27 @@ func (i *InfoBuf) Prompt(prompt string, msg string, callback func(string, bool))
i.Msg = prompt
i.HasPrompt = true
i.HasMessage, i.HasError = false, false
i.PromptCallback = callback
i.PromptCallback = donecb
i.EventCallback = eventcb
i.Buffer.Insert(i.Buffer.Start(), msg)
}
// DonePrompt finishes the current prompt and indicates whether or not it was canceled
func (i *InfoBuf) DonePrompt(canceled bool) {
i.HasPrompt = false
if canceled {
i.PromptCallback("", true)
} else {
i.PromptCallback(strings.TrimSpace(string(i.LineBytes(0))), false)
if i.PromptCallback != nil {
if canceled {
i.PromptCallback("", true)
} else {
i.PromptCallback(strings.TrimSpace(string(i.LineBytes(0))), false)
}
}
i.PromptCallback = nil
i.EventCallback = nil
i.Replace(i.Start(), i.End(), "")
}
// Reset resets the messenger's cursor, message and response
// Reset resets the infobuffer's msg and info
func (i *InfoBuf) Reset() {
i.Msg = ""
i.HasPrompt, i.HasMessage, i.HasError = false, false, false