11 "github.com/zyedidia/clipboard"
12 "github.com/zyedidia/tcell"
15 // TermMessage sends a message to the user in the terminal. This usually occurs before
16 // micro has been fully initialized -- ie if there is an error in the syntax highlighting
17 // regular expressions
18 // The function must be called when the screen is not initialized
19 // This will write the message, and wait for the user
20 // to press and key to continue
21 func TermMessage(msg ...interface{}) {
22 screenWasNil := screen == nil
28 fmt.Print("\nPress enter to continue")
30 reader := bufio.NewReader(os.Stdin)
31 reader.ReadString('\n')
38 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
40 func TermError(filename string, lineNum int, err string) {
41 TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
44 // Messenger is an object that makes it easy to send messages to the user
45 // and get input from the user
46 type Messenger struct {
47 // Are we currently prompting the user?
49 // Is there a message to print
54 // The user's response to a prompt
56 // style to use when drawing the message
59 // We have to keep track of the cursor for prompting
62 // This map stores the history for all the different kinds of uses Prompt has
63 // It's a map of history type -> history array
64 history map[string][]string
67 // Is the current message a message from the gutter
71 // Message sends a message to the user
72 func (m *Messenger) Message(msg ...interface{}) {
73 buf := new(bytes.Buffer)
74 fmt.Fprint(buf, msg...)
75 m.message = buf.String()
78 if _, ok := colorscheme["message"]; ok {
79 m.style = colorscheme["message"]
84 // Error sends an error message to the user
85 func (m *Messenger) Error(msg ...interface{}) {
86 buf := new(bytes.Buffer)
87 fmt.Fprint(buf, msg...)
88 m.message = buf.String()
90 Foreground(tcell.ColorBlack).
91 Background(tcell.ColorMaroon)
93 if _, ok := colorscheme["error-message"]; ok {
94 m.style = colorscheme["error-message"]
99 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
100 func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
104 _, h := screen.Size()
108 screen.ShowCursor(Count(m.message), h-1)
112 switch e := event.(type) {
113 case *tcell.EventKey:
118 } else if e.Rune() == 'n' {
121 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
128 // LetterPrompt gives the user a prompt and waits for a one letter response
129 func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool) {
133 _, h := screen.Size()
137 screen.ShowCursor(Count(m.message), h-1)
141 switch e := event.(type) {
142 case *tcell.EventKey:
145 for _, r := range responses {
151 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
161 NoCompletion Completion = iota
168 // Prompt sends the user a message and waits for a response to be typed in
169 // This function blocks the main loop while waiting for input
170 func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Completion) (string, bool) {
173 if _, ok := m.history[historyType]; !ok {
174 m.history[historyType] = []string{""}
176 m.history[historyType] = append(m.history[historyType], "")
178 m.historyNum = len(m.history[historyType]) - 1
180 response, canceled := "", true
184 var suggestions []string
189 switch e := event.(type) {
190 case *tcell.EventKey:
192 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
196 // User is done entering their response
198 response, canceled = m.response, false
199 m.history[historyType][len(m.history[historyType])-1] = response
201 args := strings.Split(m.response, " ")
202 currentArgNum := len(args) - 1
203 currentArg := args[currentArgNum]
204 var completionType Completion
206 if completionTypes[0] == CommandCompletion && currentArgNum > 0 {
207 if command, ok := commands[args[0]]; ok {
208 completionTypes = append([]Completion{CommandCompletion}, command.completions...)
212 if currentArgNum >= len(completionTypes) {
213 completionType = completionTypes[len(completionTypes)-1]
215 completionType = completionTypes[currentArgNum]
219 if completionType == FileCompletion {
220 chosen, suggestions = FileComplete(currentArg)
221 } else if completionType == CommandCompletion {
222 chosen, suggestions = CommandComplete(currentArg)
223 } else if completionType == HelpCompletion {
224 chosen, suggestions = HelpComplete(currentArg)
225 } else if completionType == OptionCompletion {
226 chosen, suggestions = OptionComplete(currentArg)
229 if len(suggestions) > 1 {
230 chosen = chosen + CommonSubstring(suggestions...)
235 chosen = " " + chosen
237 m.response = strings.Join(args[:len(args)-1], " ") + chosen
238 m.cursorx = Count(m.response)
243 m.HandleEvent(event, m.history[historyType])
246 for _, v := range tabs[curTab].views {
251 if len(suggestions) > 1 {
252 m.DisplaySuggestions(suggestions)
258 return response, canceled
261 // HandleEvent handles an event for the prompter
262 func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
263 switch e := event.(type) {
264 case *tcell.EventKey:
267 if m.historyNum > 0 {
269 m.response = history[m.historyNum]
270 m.cursorx = Count(m.response)
273 if m.historyNum < len(history)-1 {
275 m.response = history[m.historyNum]
276 m.cursorx = Count(m.response)
283 if m.cursorx < Count(m.response) {
286 case tcell.KeyBackspace2, tcell.KeyBackspace:
288 m.response = string([]rune(m.response)[:m.cursorx-1]) + string([]rune(m.response)[m.cursorx:])
292 clip, _ := clipboard.ReadAll()
293 m.response = Insert(m.response, m.cursorx, clip)
294 m.cursorx += Count(clip)
296 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
299 history[m.historyNum] = m.response
301 case *tcell.EventPaste:
303 m.response = Insert(m.response, m.cursorx, clip)
304 m.cursorx += Count(clip)
308 // Reset resets the messenger's cursor, message and response
309 func (m *Messenger) Reset() {
315 // Clear clears the line at the bottom of the editor
316 func (m *Messenger) Clear() {
317 w, h := screen.Size()
318 for x := 0; x < w; x++ {
319 screen.SetContent(x, h-1, ' ', nil, defStyle)
323 func (m *Messenger) DisplaySuggestions(suggestions []string) {
324 w, screenH := screen.Size()
328 statusLineStyle := defStyle.Reverse(true)
329 if style, ok := colorscheme["statusline"]; ok {
330 statusLineStyle = style
333 for x := 0; x < w; x++ {
334 screen.SetContent(x, y, ' ', nil, statusLineStyle)
338 for _, suggestion := range suggestions {
339 for _, c := range suggestion {
340 screen.SetContent(x, y, c, nil, statusLineStyle)
343 screen.SetContent(x, y, ' ', nil, statusLineStyle)
348 // Display displays messages or prompts
349 func (m *Messenger) Display() {
350 _, h := screen.Size()
352 if m.hasPrompt || globalSettings["infobar"].(bool) {
353 runes := []rune(m.message + m.response)
354 for x := 0; x < len(runes); x++ {
355 screen.SetContent(x, h-1, runes[x], nil, m.style)
361 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
366 // A GutterMessage is a message displayed on the side of the editor
367 type GutterMessage struct {
373 // These are the different types of messages
375 // GutterInfo represents a simple info message
377 // GutterWarning represents a compiler warning
379 // GutterError represents a compiler error