6 "github.com/zyedidia/micro/internal/buffer"
9 // The InfoBuf displays messages and other info at the bottom of the screen.
10 // It is respresented as a buffer and a message with a style.
24 // This map stores the history for all the different kinds of uses Prompt has
25 // It's a map of history type -> history array
26 History map[string][]string
29 // Is the current message a message from the gutter
32 PromptCallback func(resp string, canceled bool)
33 EventCallback func(resp string)
34 YNCallback func(yes bool, canceled bool)
37 // NewBuffer returns a new infobuffer
38 func NewBuffer() *InfoBuf {
40 ib.History = make(map[string][]string)
42 ib.Buffer = buffer.NewBufferFromString("", "", buffer.BTInfo)
48 // Close performs any cleanup necessary when shutting down the infobuffer
49 func (i *InfoBuf) Close() {
53 // Message sends a message to the user
54 func (i *InfoBuf) Message(msg ...interface{}) {
55 // only display a new message if there isn't an active prompt
56 // this is to prevent overwriting an existing prompt to the user
57 if i.HasPrompt == false {
58 displayMessage := fmt.Sprint(msg...)
59 // if there is no active prompt then style and display the message as normal
60 i.Msg = displayMessage
61 i.HasMessage, i.HasError = true, false
65 // GutterMessage displays a message and marks it as a gutter message
66 func (i *InfoBuf) GutterMessage(msg ...interface{}) {
71 // ClearGutter clears the info bar and unmarks the message
72 func (i *InfoBuf) ClearGutter() {
77 // Error sends an error message to the user
78 func (i *InfoBuf) Error(msg ...interface{}) {
79 // only display a new message if there isn't an active prompt
80 // this is to prevent overwriting an existing prompt to the user
81 if i.HasPrompt == false {
82 // if there is no active prompt then style and display the message as normal
83 i.Msg = fmt.Sprint(msg...)
84 i.HasMessage, i.HasError = false, true
89 // Prompt starts a prompt for the user, it takes a prompt, a possibly partially filled in msg
90 // and callbacks executed when the user executes an event and when the user finishes the prompt
91 // The eventcb passes the current user response as the argument and donecb passes the user's message
92 // and a boolean indicating if the prompt was canceled
93 func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(string), donecb func(string, bool)) {
94 // If we get another prompt mid-prompt we cancel the one getting overwritten
99 if _, ok := i.History[ptype]; !ok {
100 i.History[ptype] = []string{""}
102 i.History[ptype] = append(i.History[ptype], "")
104 i.HistoryNum = len(i.History[ptype]) - 1
109 i.HasMessage, i.HasError, i.HasYN = false, false, false
111 i.PromptCallback = donecb
112 i.EventCallback = eventcb
113 i.Buffer.Insert(i.Buffer.Start(), msg)
116 func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
124 i.HasMessage, i.HasError = false, false
126 i.YNCallback = donecb
129 // DonePrompt finishes the current prompt and indicates whether or not it was canceled
130 func (i *InfoBuf) DonePrompt(canceled bool) {
136 if i.PromptCallback != nil {
138 i.PromptCallback("", true)
139 h := i.History[i.PromptType]
140 i.History[i.PromptType] = h[:len(h)-1]
142 resp := string(i.LineBytes(0))
143 i.PromptCallback(resp, false)
144 h := i.History[i.PromptType]
147 i.PromptCallback = nil
149 i.Replace(i.Start(), i.End(), "")
151 if i.YNCallback != nil && hadYN {
152 i.YNCallback(i.YNResp, canceled)
156 // Reset resets the infobuffer's msg and info
157 func (i *InfoBuf) Reset() {
159 i.HasPrompt, i.HasMessage, i.HasError = false, false, false