]> git.lizzy.rs Git - micro.git/blob - cmd/micro/messenger.go
1010882033720a81082813ab7c122f3c2a462d4a
[micro.git] / cmd / micro / messenger.go
1 package main
2
3 import (
4         "bufio"
5         "bytes"
6         "fmt"
7         "os"
8         "strconv"
9
10         "github.com/zyedidia/tcell"
11 )
12
13 // TermMessage sends a message to the user in the terminal. This usually occurs before
14 // micro has been fully initialized -- ie if there is an error in the syntax highlighting
15 // regular expressions
16 // The function must be called when the screen is not initialized
17 // This will write the message, and wait for the user
18 // to press and key to continue
19 func TermMessage(msg ...interface{}) {
20         screenWasNil := screen == nil
21         if !screenWasNil {
22                 screen.Fini()
23         }
24
25         fmt.Println(msg...)
26         fmt.Print("\nPress enter to continue")
27
28         reader := bufio.NewReader(os.Stdin)
29         reader.ReadString('\n')
30
31         if !screenWasNil {
32                 InitScreen()
33         }
34 }
35
36 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
37 // as an error
38 func TermError(filename string, lineNum int, err string) {
39         TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
40 }
41
42 // Messenger is an object that makes it easy to send messages to the user
43 // and get input from the user
44 type Messenger struct {
45         // Are we currently prompting the user?
46         hasPrompt bool
47         // Is there a message to print
48         hasMessage bool
49
50         // Message to print
51         message string
52         // The user's response to a prompt
53         response string
54         // style to use when drawing the message
55         style tcell.Style
56
57         // We have to keep track of the cursor for prompting
58         cursorx int
59 }
60
61 // Message sends a message to the user
62 func (m *Messenger) Message(msg ...interface{}) {
63         buf := new(bytes.Buffer)
64         fmt.Fprint(buf, msg...)
65         m.message = buf.String()
66         m.style = defStyle
67
68         if _, ok := colorscheme["message"]; ok {
69                 m.style = colorscheme["message"]
70         }
71         m.hasMessage = true
72 }
73
74 // Error sends an error message to the user
75 func (m *Messenger) Error(msg ...interface{}) {
76         buf := new(bytes.Buffer)
77         fmt.Fprint(buf, msg...)
78         m.message = buf.String()
79         m.style = defStyle.
80                 Foreground(tcell.ColorBlack).
81                 Background(tcell.ColorMaroon)
82
83         if _, ok := colorscheme["error-message"]; ok {
84                 m.style = colorscheme["error-message"]
85         }
86         m.hasMessage = true
87 }
88
89 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
90 func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
91         m.Message(prompt)
92
93         for {
94                 m.Clear()
95                 m.Display()
96                 screen.Show()
97                 event := screen.PollEvent()
98
99                 switch e := event.(type) {
100                 case *tcell.EventKey:
101                         switch e.Key() {
102                         case tcell.KeyRune:
103                                 if e.Rune() == 'y' {
104                                         return true, false
105                                 } else if e.Rune() == 'n' {
106                                         return false, false
107                                 }
108                         case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
109                                 return false, true
110                         }
111                 }
112         }
113 }
114
115 // Prompt sends the user a message and waits for a response to be typed in
116 // This function blocks the main loop while waiting for input
117 func (m *Messenger) Prompt(prompt string) (string, bool) {
118         m.hasPrompt = true
119         m.Message(prompt)
120
121         response, canceled := "", true
122
123         for m.hasPrompt {
124                 m.Clear()
125                 m.Display()
126
127                 event := screen.PollEvent()
128
129                 switch e := event.(type) {
130                 case *tcell.EventKey:
131                         switch e.Key() {
132                         case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
133                                 // Cancel
134                                 m.hasPrompt = false
135                         case tcell.KeyEnter:
136                                 // User is done entering their response
137                                 m.hasPrompt = false
138                                 response, canceled = m.response, false
139                         }
140                 }
141
142                 m.HandleEvent(event)
143
144                 if m.cursorx < 0 {
145                         // Cancel
146                         m.hasPrompt = false
147                 }
148         }
149
150         m.Reset()
151         return response, canceled
152 }
153
154 // HandleEvent handles an event for the prompter
155 func (m *Messenger) HandleEvent(event tcell.Event) {
156         switch e := event.(type) {
157         case *tcell.EventKey:
158                 switch e.Key() {
159                 case tcell.KeyLeft:
160                         if m.cursorx > 0 {
161                                 m.cursorx--
162                         }
163                 case tcell.KeyRight:
164                         if m.cursorx < Count(m.response) {
165                                 m.cursorx++
166                         }
167                 case tcell.KeyBackspace2:
168                         if m.cursorx > 0 {
169                                 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
170                         }
171                         m.cursorx--
172                 case tcell.KeySpace:
173                         m.response += " "
174                         m.cursorx++
175                 case tcell.KeyRune:
176                         m.response = Insert(m.response, m.cursorx, string(e.Rune()))
177                         m.cursorx++
178                 }
179         }
180 }
181
182 // Reset resets the messenger's cursor, message and response
183 func (m *Messenger) Reset() {
184         m.cursorx = 0
185         m.message = ""
186         m.response = ""
187 }
188
189 // Clear clears the line at the bottom of the editor
190 func (m *Messenger) Clear() {
191         w, h := screen.Size()
192         for x := 0; x < w; x++ {
193                 screen.SetContent(x, h-1, ' ', nil, defStyle)
194         }
195 }
196
197 // Display displays messages or prompts
198 func (m *Messenger) Display() {
199         _, h := screen.Size()
200         if m.hasMessage {
201                 runes := []rune(m.message + m.response)
202                 for x := 0; x < len(runes); x++ {
203                         screen.SetContent(x, h-1, runes[x], nil, m.style)
204                 }
205         }
206         if m.hasPrompt {
207                 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
208                 screen.Show()
209         }
210 }