]> git.lizzy.rs Git - micro.git/blob - cmd/micro/info/infobuffer.go
Fix infobar prompt
[micro.git] / cmd / micro / info / infobuffer.go
1 package info
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/zyedidia/micro/cmd/micro/buffer"
8 )
9
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.
12 type InfoBuf struct {
13         *buffer.Buffer
14
15         HasPrompt  bool
16         HasMessage bool
17         HasError   bool
18
19         Msg string
20
21         // This map stores the history for all the different kinds of uses Prompt has
22         // It's a map of history type -> history array
23         History    map[string][]string
24         HistoryNum int
25
26         // Is the current message a message from the gutter
27         GutterMessage bool
28
29         PromptCallback func(resp string, canceled bool)
30 }
31
32 func NewBuffer() *InfoBuf {
33         ib := new(InfoBuf)
34         ib.History = make(map[string][]string)
35
36         ib.Buffer = buffer.NewBufferFromString("", "infobar", buffer.BTInfo)
37
38         return ib
39 }
40
41 // Message sends a message to the user
42 func (i *InfoBuf) Message(msg ...interface{}) {
43         // only display a new message if there isn't an active prompt
44         // this is to prevent overwriting an existing prompt to the user
45         if i.HasPrompt == false {
46                 displayMessage := fmt.Sprint(msg...)
47                 // if there is no active prompt then style and display the message as normal
48                 i.Msg = displayMessage
49                 i.HasMessage = true
50         }
51 }
52
53 // Error sends an error message to the user
54 func (i *InfoBuf) Error(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                 // if there is no active prompt then style and display the message as normal
59                 i.Msg = fmt.Sprint(msg...)
60                 i.HasMessage, i.HasError = false, true
61         }
62         // TODO: add to log?
63 }
64
65 func (i *InfoBuf) Prompt(prompt string, msg string, callback func(string, bool)) {
66         // If we get another prompt mid-prompt we cancel the one getting overwritten
67         if i.HasPrompt {
68                 i.DonePrompt(true)
69         }
70
71         i.Msg = prompt
72         i.HasPrompt = true
73         i.HasMessage, i.HasError = false, false
74         i.PromptCallback = callback
75         i.Buffer.Insert(i.Buffer.Start(), msg)
76 }
77
78 func (i *InfoBuf) DonePrompt(canceled bool) {
79         i.HasPrompt = false
80         if canceled {
81                 i.PromptCallback("", true)
82         } else {
83                 i.PromptCallback(strings.TrimSpace(string(i.LineBytes(0))), false)
84         }
85         i.Replace(i.Start(), i.End(), "")
86 }
87
88 // Reset resets the messenger's cursor, message and response
89 func (i *InfoBuf) Reset() {
90         i.Msg = ""
91         i.HasPrompt, i.HasMessage, i.HasError = false, false, false
92 }