11 "github.com/zyedidia/tcell"
14 // TermMessage sends a message to the user in the terminal. This usually occurs before
15 // micro has been fully initialized -- ie if there is an error in the syntax highlighting
16 // regular expressions
17 // The function must be called when the screen is not initialized
18 // This will write the message, and wait for the user
19 // to press and key to continue
20 func TermMessage(msg ...interface{}) {
21 screenWasNil := screen == nil
27 fmt.Print("\nPress enter to continue")
29 reader := bufio.NewReader(os.Stdin)
30 reader.ReadString('\n')
37 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
39 func TermError(filename string, lineNum int, err string) {
40 TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
43 // Messenger is an object that makes it easy to send messages to the user
44 // and get input from the user
45 type Messenger struct {
46 // Are we currently prompting the user?
48 // Is there a message to print
53 // The user's response to a prompt
55 // style to use when drawing the message
58 // We have to keep track of the cursor for prompting
61 // This map stores the history for all the different kinds of uses Prompt has
62 // It's a map of history type -> history array
63 history map[string][]string
66 // Is the current message a message from the gutter
70 // Message sends a message to the user
71 func (m *Messenger) Message(msg ...interface{}) {
72 buf := new(bytes.Buffer)
73 fmt.Fprint(buf, msg...)
74 m.message = buf.String()
77 if _, ok := colorscheme["message"]; ok {
78 m.style = colorscheme["message"]
83 // Error sends an error message to the user
84 func (m *Messenger) Error(msg ...interface{}) {
85 buf := new(bytes.Buffer)
86 fmt.Fprint(buf, msg...)
87 m.message = buf.String()
89 Foreground(tcell.ColorBlack).
90 Background(tcell.ColorMaroon)
92 if _, ok := colorscheme["error-message"]; ok {
93 m.style = colorscheme["error-message"]
98 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
99 func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
102 _, h := screen.Size()
106 screen.ShowCursor(Count(m.message), h-1)
110 switch e := event.(type) {
111 case *tcell.EventKey:
116 } else if e.Rune() == 'n' {
119 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
129 NoCompletion Completion = iota
136 // Prompt sends the user a message and waits for a response to be typed in
137 // This function blocks the main loop while waiting for input
138 func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Completion) (string, bool) {
141 if _, ok := m.history[historyType]; !ok {
142 m.history[historyType] = []string{""}
144 m.history[historyType] = append(m.history[historyType], "")
146 m.historyNum = len(m.history[historyType]) - 1
148 response, canceled := "", true
152 var suggestions []string
157 switch e := event.(type) {
158 case *tcell.EventKey:
160 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
164 // User is done entering their response
166 response, canceled = m.response, false
167 m.history[historyType][len(m.history[historyType])-1] = response
169 args := strings.Split(m.response, " ")
170 currentArgNum := len(args) - 1
171 currentArg := args[currentArgNum]
172 var completionType Completion
174 if completionTypes[0] == CommandCompletion && currentArgNum > 0 {
175 if command, ok := commands[args[0]]; ok {
176 completionTypes = append([]Completion{CommandCompletion}, command.completions...)
180 if currentArgNum >= len(completionTypes) {
181 completionType = completionTypes[len(completionTypes)-1]
183 completionType = completionTypes[currentArgNum]
187 if completionType == FileCompletion {
188 chosen, suggestions = FileComplete(currentArg)
189 } else if completionType == CommandCompletion {
190 chosen, suggestions = CommandComplete(currentArg)
191 } else if completionType == HelpCompletion {
192 chosen, suggestions = HelpComplete(currentArg)
193 } else if completionType == OptionCompletion {
194 chosen, suggestions = OptionComplete(currentArg)
199 chosen = " " + chosen
201 m.response = strings.Join(args[:len(args)-1], " ") + chosen
202 m.cursorx = Count(m.response)
207 m.HandleEvent(event, m.history[historyType])
210 for _, v := range tabs[curTab].views {
215 if len(suggestions) > 1 {
216 m.DisplaySuggestions(suggestions)
222 return response, canceled
225 // HandleEvent handles an event for the prompter
226 func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
227 switch e := event.(type) {
228 case *tcell.EventKey:
231 if m.historyNum > 0 {
233 m.response = history[m.historyNum]
234 m.cursorx = Count(m.response)
237 if m.historyNum < len(history)-1 {
239 m.response = history[m.historyNum]
240 m.cursorx = Count(m.response)
247 if m.cursorx < Count(m.response) {
250 case tcell.KeyBackspace2, tcell.KeyBackspace:
252 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
256 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
259 history[m.historyNum] = m.response
263 // Reset resets the messenger's cursor, message and response
264 func (m *Messenger) Reset() {
270 // Clear clears the line at the bottom of the editor
271 func (m *Messenger) Clear() {
272 w, h := screen.Size()
273 for x := 0; x < w; x++ {
274 screen.SetContent(x, h-1, ' ', nil, defStyle)
278 func (m *Messenger) DisplaySuggestions(suggestions []string) {
279 w, screenH := screen.Size()
282 for x := 0; x < w; x++ {
283 screen.SetContent(x, y, ' ', nil, defStyle.Reverse(true))
287 for _, suggestion := range suggestions {
288 for _, c := range suggestion {
289 screen.SetContent(x, y, c, nil, defStyle.Reverse(true))
292 screen.SetContent(x, y, ' ', nil, defStyle.Reverse(true))
297 // Display displays messages or prompts
298 func (m *Messenger) Display() {
299 _, h := screen.Size()
301 runes := []rune(m.message + m.response)
302 for x := 0; x < len(runes); x++ {
303 screen.SetContent(x, h-1, runes[x], nil, m.style)
307 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
312 // A GutterMessage is a message displayed on the side of the editor
313 type GutterMessage struct {
319 // These are the different types of messages
321 // GutterInfo represents a simple info message
323 // GutterWarning represents a compiler warning
325 // GutterError represents a compiler error