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) {
103 _, h := screen.Size()
107 screen.ShowCursor(Count(m.message), h-1)
111 switch e := event.(type) {
112 case *tcell.EventKey:
117 } else if e.Rune() == 'n' {
120 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
130 NoCompletion Completion = iota
137 // Prompt sends the user a message and waits for a response to be typed in
138 // This function blocks the main loop while waiting for input
139 func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Completion) (string, bool) {
142 if _, ok := m.history[historyType]; !ok {
143 m.history[historyType] = []string{""}
145 m.history[historyType] = append(m.history[historyType], "")
147 m.historyNum = len(m.history[historyType]) - 1
149 response, canceled := "", true
153 var suggestions []string
158 switch e := event.(type) {
159 case *tcell.EventKey:
161 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
165 // User is done entering their response
167 response, canceled = m.response, false
168 m.history[historyType][len(m.history[historyType])-1] = response
170 args := strings.Split(m.response, " ")
171 currentArgNum := len(args) - 1
172 currentArg := args[currentArgNum]
173 var completionType Completion
175 if completionTypes[0] == CommandCompletion && currentArgNum > 0 {
176 if command, ok := commands[args[0]]; ok {
177 completionTypes = append([]Completion{CommandCompletion}, command.completions...)
181 if currentArgNum >= len(completionTypes) {
182 completionType = completionTypes[len(completionTypes)-1]
184 completionType = completionTypes[currentArgNum]
188 if completionType == FileCompletion {
189 chosen, suggestions = FileComplete(currentArg)
190 } else if completionType == CommandCompletion {
191 chosen, suggestions = CommandComplete(currentArg)
192 } else if completionType == HelpCompletion {
193 chosen, suggestions = HelpComplete(currentArg)
194 } else if completionType == OptionCompletion {
195 chosen, suggestions = OptionComplete(currentArg)
200 chosen = " " + chosen
202 m.response = strings.Join(args[:len(args)-1], " ") + chosen
203 m.cursorx = Count(m.response)
208 m.HandleEvent(event, m.history[historyType])
211 for _, v := range tabs[curTab].views {
216 if len(suggestions) > 1 {
217 m.DisplaySuggestions(suggestions)
223 return response, canceled
226 // HandleEvent handles an event for the prompter
227 func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
228 switch e := event.(type) {
229 case *tcell.EventKey:
232 if m.historyNum > 0 {
234 m.response = history[m.historyNum]
235 m.cursorx = Count(m.response)
238 if m.historyNum < len(history)-1 {
240 m.response = history[m.historyNum]
241 m.cursorx = Count(m.response)
248 if m.cursorx < Count(m.response) {
251 case tcell.KeyBackspace2, tcell.KeyBackspace:
253 m.response = string([]rune(m.response)[:m.cursorx-1]) + string([]rune(m.response)[m.cursorx:])
257 clip, _ := clipboard.ReadAll()
258 m.response = Insert(m.response, m.cursorx, clip)
259 m.cursorx += Count(clip)
261 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
264 history[m.historyNum] = m.response
266 case *tcell.EventPaste:
268 m.response = Insert(m.response, m.cursorx, clip)
269 m.cursorx += Count(clip)
273 // Reset resets the messenger's cursor, message and response
274 func (m *Messenger) Reset() {
280 // Clear clears the line at the bottom of the editor
281 func (m *Messenger) Clear() {
282 w, h := screen.Size()
283 for x := 0; x < w; x++ {
284 screen.SetContent(x, h-1, ' ', nil, defStyle)
288 func (m *Messenger) DisplaySuggestions(suggestions []string) {
289 w, screenH := screen.Size()
293 statusLineStyle := defStyle.Reverse(true)
294 if style, ok := colorscheme["statusline"]; ok {
295 statusLineStyle = style
298 for x := 0; x < w; x++ {
299 screen.SetContent(x, y, ' ', nil, statusLineStyle)
303 for _, suggestion := range suggestions {
304 for _, c := range suggestion {
305 screen.SetContent(x, y, c, nil, statusLineStyle)
308 screen.SetContent(x, y, ' ', nil, statusLineStyle)
313 // Display displays messages or prompts
314 func (m *Messenger) Display() {
315 _, h := screen.Size()
317 runes := []rune(m.message + m.response)
318 for x := 0; x < len(runes); x++ {
319 screen.SetContent(x, h-1, runes[x], nil, m.style)
323 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
328 // A GutterMessage is a message displayed on the side of the editor
329 type GutterMessage struct {
335 // These are the different types of messages
337 // GutterInfo represents a simple info message
339 // GutterWarning represents a compiler warning
341 // GutterError represents a compiler error