]> git.lizzy.rs Git - micro.git/blob - internal/info/infobuffer.go
No backups for no name files
[micro.git] / internal / info / infobuffer.go
1 package info
2
3 import (
4         "fmt"
5
6         "github.com/zyedidia/micro/internal/buffer"
7 )
8
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.
11 type InfoBuf struct {
12         *buffer.Buffer
13
14         HasPrompt  bool
15         HasMessage bool
16         HasError   bool
17         HasYN      bool
18
19         PromptType string
20
21         Msg    string
22         YNResp bool
23
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
27         HistoryNum int
28
29         // Is the current message a message from the gutter
30         HasGutter bool
31
32         PromptCallback func(resp string, canceled bool)
33         EventCallback  func(resp string)
34         YNCallback     func(yes bool, canceled bool)
35 }
36
37 // NewBuffer returns a new infobuffer
38 func NewBuffer() *InfoBuf {
39         ib := new(InfoBuf)
40         ib.History = make(map[string][]string)
41
42         ib.Buffer = buffer.NewBufferFromString("", "", buffer.BTInfo)
43         ib.LoadHistory()
44
45         return ib
46 }
47
48 // Close performs any cleanup necessary when shutting down the infobuffer
49 func (i *InfoBuf) Close() {
50         i.SaveHistory()
51 }
52
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
62         }
63 }
64
65 // GutterMessage displays a message and marks it as a gutter message
66 func (i *InfoBuf) GutterMessage(msg ...interface{}) {
67         i.Message(msg...)
68         i.HasGutter = true
69 }
70
71 // ClearGutter clears the info bar and unmarks the message
72 func (i *InfoBuf) ClearGutter() {
73         i.HasGutter = false
74         i.Message("")
75 }
76
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
85         }
86         // TODO: add to log?
87 }
88
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
95         if i.HasPrompt {
96                 i.DonePrompt(true)
97         }
98
99         if _, ok := i.History[ptype]; !ok {
100                 i.History[ptype] = []string{""}
101         } else {
102                 i.History[ptype] = append(i.History[ptype], "")
103         }
104         i.HistoryNum = len(i.History[ptype]) - 1
105
106         i.PromptType = ptype
107         i.Msg = prompt
108         i.HasPrompt = true
109         i.HasMessage, i.HasError, i.HasYN = false, false, false
110         i.HasGutter = false
111         i.PromptCallback = donecb
112         i.EventCallback = eventcb
113         i.Buffer.Insert(i.Buffer.Start(), msg)
114 }
115
116 func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
117         if i.HasPrompt {
118                 i.DonePrompt(true)
119         }
120
121         i.Msg = prompt
122         i.HasPrompt = true
123         i.HasYN = true
124         i.HasMessage, i.HasError = false, false
125         i.HasGutter = false
126         i.YNCallback = donecb
127 }
128
129 // DonePrompt finishes the current prompt and indicates whether or not it was canceled
130 func (i *InfoBuf) DonePrompt(canceled bool) {
131         hadYN := i.HasYN
132         i.HasPrompt = false
133         i.HasYN = false
134         i.HasGutter = false
135         if !hadYN {
136                 if i.PromptCallback != nil {
137                         if canceled {
138                                 i.PromptCallback("", true)
139                                 h := i.History[i.PromptType]
140                                 i.History[i.PromptType] = h[:len(h)-1]
141                         } else {
142                                 resp := string(i.LineBytes(0))
143                                 i.PromptCallback(resp, false)
144                                 h := i.History[i.PromptType]
145                                 h[len(h)-1] = resp
146                         }
147                         i.PromptCallback = nil
148                 }
149                 i.Replace(i.Start(), i.End(), "")
150         }
151         if i.YNCallback != nil && hadYN {
152                 i.YNCallback(i.YNResp, canceled)
153         }
154 }
155
156 // Reset resets the infobuffer's msg and info
157 func (i *InfoBuf) Reset() {
158         i.Msg = ""
159         i.HasPrompt, i.HasMessage, i.HasError = false, false, false
160         i.HasGutter = false
161 }