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
156 switch e := event.(type) {
157 case *tcell.EventKey:
159 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
163 // User is done entering their response
165 response, canceled = m.response, false
166 m.history[historyType][len(m.history[historyType])-1] = response
168 args := strings.Split(m.response, " ")
169 currentArgNum := len(args) - 1
170 currentArg := args[currentArgNum]
171 var completionType Completion
173 if completionTypes[0] == CommandCompletion && len(completionTypes) == 1 && currentArgNum > 0 {
174 if command, ok := commands[args[0]]; ok {
175 completionTypes = append(completionTypes, command.completions...)
179 if currentArgNum >= len(completionTypes) {
180 completionType = completionTypes[len(completionTypes)-1]
182 completionType = completionTypes[currentArgNum]
186 if completionType == FileCompletion {
187 chosen, _ = FileComplete(currentArg)
188 } else if completionType == CommandCompletion {
189 chosen, _ = CommandComplete(currentArg)
190 } else if completionType == HelpCompletion {
191 chosen, _ = HelpComplete(currentArg)
192 } else if completionType == OptionCompletion {
193 chosen, _ = OptionComplete(currentArg)
198 chosen = " " + chosen
200 m.response = strings.Join(args[:len(args)-1], " ") + chosen
201 m.cursorx = Count(m.response)
206 m.HandleEvent(event, m.history[historyType])
210 return response, canceled
213 // HandleEvent handles an event for the prompter
214 func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
215 switch e := event.(type) {
216 case *tcell.EventKey:
219 if m.historyNum > 0 {
221 m.response = history[m.historyNum]
222 m.cursorx = Count(m.response)
225 if m.historyNum < len(history)-1 {
227 m.response = history[m.historyNum]
228 m.cursorx = Count(m.response)
235 if m.cursorx < Count(m.response) {
238 case tcell.KeyBackspace2, tcell.KeyBackspace:
240 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
244 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
247 history[m.historyNum] = m.response
251 // Reset resets the messenger's cursor, message and response
252 func (m *Messenger) Reset() {
258 // Clear clears the line at the bottom of the editor
259 func (m *Messenger) Clear() {
260 w, h := screen.Size()
261 for x := 0; x < w; x++ {
262 screen.SetContent(x, h-1, ' ', nil, defStyle)
266 // Display displays messages or prompts
267 func (m *Messenger) Display() {
268 _, h := screen.Size()
270 runes := []rune(m.message + m.response)
271 for x := 0; x < len(runes); x++ {
272 screen.SetContent(x, h-1, runes[x], nil, m.style)
276 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
281 // A GutterMessage is a message displayed on the side of the editor
282 type GutterMessage struct {
288 // These are the different types of messages
290 // GutterInfo represents a simple info message
292 // GutterWarning represents a compiler warning
294 // GutterError represents a compiler error