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
134 // Prompt sends the user a message and waits for a response to be typed in
135 // This function blocks the main loop while waiting for input
136 func (m *Messenger) Prompt(prompt, historyType string, completionType Completion) (string, bool) {
139 if _, ok := m.history[historyType]; !ok {
140 m.history[historyType] = []string{""}
142 m.history[historyType] = append(m.history[historyType], "")
144 m.historyNum = len(m.history[historyType]) - 1
146 response, canceled := "", true
154 switch e := event.(type) {
155 case *tcell.EventKey:
157 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
161 // User is done entering their response
163 response, canceled = m.response, false
164 m.history[historyType][len(m.history[historyType])-1] = response
166 if completionType == FileCompletion {
167 dirs := strings.Split(m.response, "/")
168 var files []os.FileInfo
171 files, err = ioutil.ReadDir(strings.Join(dirs[:len(dirs)-1], "/"))
173 files, err = ioutil.ReadDir(".")
178 var suggestions []string
179 for _, f := range files {
184 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
185 suggestions = append(suggestions, name)
188 if len(suggestions) == 1 {
190 m.response = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
192 m.response = suggestions[0]
194 m.cursorx = Count(m.response)
200 m.HandleEvent(event, m.history[historyType])
209 return response, canceled
212 // HandleEvent handles an event for the prompter
213 func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
214 switch e := event.(type) {
215 case *tcell.EventKey:
218 if m.historyNum > 0 {
220 m.response = history[m.historyNum]
221 m.cursorx = Count(m.response)
224 if m.historyNum < len(history)-1 {
226 m.response = history[m.historyNum]
227 m.cursorx = Count(m.response)
234 if m.cursorx < Count(m.response) {
237 case tcell.KeyBackspace2, tcell.KeyBackspace:
239 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
243 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
246 history[m.historyNum] = m.response
250 // Reset resets the messenger's cursor, message and response
251 func (m *Messenger) Reset() {
257 // Clear clears the line at the bottom of the editor
258 func (m *Messenger) Clear() {
259 w, h := screen.Size()
260 for x := 0; x < w; x++ {
261 screen.SetContent(x, h-1, ' ', nil, defStyle)
265 // Display displays messages or prompts
266 func (m *Messenger) Display() {
267 _, h := screen.Size()
269 runes := []rune(m.message + m.response)
270 for x := 0; x < len(runes); x++ {
271 screen.SetContent(x, h-1, runes[x], nil, m.style)
275 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
280 // A GutterMessage is a message displayed on the side of the editor
281 type GutterMessage struct {
287 // These are the different types of messages
289 // GutterInfo represents a simple info message
291 // GutterWarning represents a compiler warning
293 // GutterError represents a compiler error