7 "github.com/zyedidia/micro/cmd/micro/buffer"
10 // The InfoBuf displays messages and other info at the bottom of the screen.
11 // It is respresented as a buffer and a message with a style.
25 // This map stores the history for all the different kinds of uses Prompt has
26 // It's a map of history type -> history array
27 History map[string][]string
30 // Is the current message a message from the gutter
33 PromptCallback func(resp string, canceled bool)
34 EventCallback func(resp string)
35 YNCallback func(yes bool, canceled bool)
38 // NewBuffer returns a new infobuffer
39 func NewBuffer() *InfoBuf {
41 ib.History = make(map[string][]string)
43 ib.Buffer = buffer.NewBufferFromString("", "infobar", buffer.BTInfo)
49 // Close performs any cleanup necessary when shutting down the infobuffer
50 func (i *InfoBuf) Close() {
54 // Message sends a message to the user
55 func (i *InfoBuf) Message(msg ...interface{}) {
56 // only display a new message if there isn't an active prompt
57 // this is to prevent overwriting an existing prompt to the user
58 if i.HasPrompt == false {
59 displayMessage := fmt.Sprint(msg...)
60 // if there is no active prompt then style and display the message as normal
61 i.Msg = displayMessage
66 // GutterMessage displays a message and marks it as a gutter message
67 func (i *InfoBuf) GutterMessage(msg ...interface{}) {
72 // ClearGutter clears the info bar and unmarks the message
73 func (i *InfoBuf) ClearGutter() {
78 // Error sends an error message to the user
79 func (i *InfoBuf) Error(msg ...interface{}) {
80 // only display a new message if there isn't an active prompt
81 // this is to prevent overwriting an existing prompt to the user
82 if i.HasPrompt == false {
83 // if there is no active prompt then style and display the message as normal
84 i.Msg = fmt.Sprint(msg...)
85 i.HasMessage, i.HasError = false, true
90 // Prompt starts a prompt for the user, it takes a prompt, a possibly partially filled in msg
91 // and callbacks executed when the user executes an event and when the user finishes the prompt
92 // The eventcb passes the current user response as the argument and donecb passes the user's message
93 // and a boolean indicating if the prompt was canceled
94 func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(string), donecb func(string, bool)) {
95 // If we get another prompt mid-prompt we cancel the one getting overwritten
100 if _, ok := i.History[ptype]; !ok {
101 i.History[ptype] = []string{""}
103 i.History[ptype] = append(i.History[ptype], "")
105 i.HistoryNum = len(i.History[ptype]) - 1
110 i.HasMessage, i.HasError, i.HasYN = false, false, false
112 i.PromptCallback = donecb
113 i.EventCallback = eventcb
114 i.Buffer.Insert(i.Buffer.Start(), []byte(msg))
117 func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
125 i.HasMessage, i.HasError = false, false
127 i.YNCallback = donecb
130 // DonePrompt finishes the current prompt and indicates whether or not it was canceled
131 func (i *InfoBuf) DonePrompt(canceled bool) {
137 if i.PromptCallback != nil {
139 i.PromptCallback("", true)
140 h := i.History[i.PromptType]
141 i.History[i.PromptType] = h[:len(h)-1]
143 resp := strings.TrimSpace(string(i.LineBytes(0)))
144 i.PromptCallback(resp, false)
145 h := i.History[i.PromptType]
148 i.PromptCallback = nil
149 i.EventCallback = nil
151 if i.EventCallback != nil {
152 i.EventCallback = nil
154 i.Replace(i.Start(), i.End(), []byte{})
156 if i.YNCallback != nil && hadYN {
157 i.YNCallback(i.YNResp, canceled)
162 // Reset resets the infobuffer's msg and info
163 func (i *InfoBuf) Reset() {
165 i.HasPrompt, i.HasMessage, i.HasError = false, false, false