10 "github.com/zyedidia/tcell"
13 // TermMessage sends a message to the user in the terminal. This usually occurs before
14 // micro has been fully initialized -- ie if there is an error in the syntax highlighting
15 // regular expressions
16 // The function must be called when the screen is not initialized
17 // This will write the message, and wait for the user
18 // to press and key to continue
19 func TermMessage(msg ...interface{}) {
20 screenWasNil := screen == nil
26 fmt.Print("\nPress enter to continue")
28 reader := bufio.NewReader(os.Stdin)
29 reader.ReadString('\n')
36 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
38 func TermError(filename string, lineNum int, err string) {
39 TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
42 // Messenger is an object that makes it easy to send messages to the user
43 // and get input from the user
44 type Messenger struct {
45 // Are we currently prompting the user?
47 // Is there a message to print
52 // The user's response to a prompt
54 // style to use when drawing the message
57 // We have to keep track of the cursor for prompting
60 // Is the current message a message from the gutter
64 // Message sends a message to the user
65 func (m *Messenger) Message(msg ...interface{}) {
66 buf := new(bytes.Buffer)
67 fmt.Fprint(buf, msg...)
68 m.message = buf.String()
71 if _, ok := colorscheme["message"]; ok {
72 m.style = colorscheme["message"]
77 // Error sends an error message to the user
78 func (m *Messenger) Error(msg ...interface{}) {
79 buf := new(bytes.Buffer)
80 fmt.Fprint(buf, msg...)
81 m.message = buf.String()
83 Foreground(tcell.ColorBlack).
84 Background(tcell.ColorMaroon)
86 if _, ok := colorscheme["error-message"]; ok {
87 m.style = colorscheme["error-message"]
92 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
93 func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
100 event := screen.PollEvent()
102 switch e := event.(type) {
103 case *tcell.EventKey:
108 } else if e.Rune() == 'n' {
111 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
118 // Prompt sends the user a message and waits for a response to be typed in
119 // This function blocks the main loop while waiting for input
120 func (m *Messenger) Prompt(prompt string) (string, bool) {
124 response, canceled := "", true
130 event := screen.PollEvent()
132 switch e := event.(type) {
133 case *tcell.EventKey:
135 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
139 // User is done entering their response
141 response, canceled = m.response, false
154 return response, canceled
157 // HandleEvent handles an event for the prompter
158 func (m *Messenger) HandleEvent(event tcell.Event) {
159 switch e := event.(type) {
160 case *tcell.EventKey:
167 if m.cursorx < Count(m.response) {
170 case tcell.KeyBackspace2, tcell.KeyBackspace:
172 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
176 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
182 // Reset resets the messenger's cursor, message and response
183 func (m *Messenger) Reset() {
189 // Clear clears the line at the bottom of the editor
190 func (m *Messenger) Clear() {
191 w, h := screen.Size()
192 for x := 0; x < w; x++ {
193 screen.SetContent(x, h-1, ' ', nil, defStyle)
197 // Display displays messages or prompts
198 func (m *Messenger) Display() {
199 _, h := screen.Size()
201 runes := []rune(m.message + m.response)
202 for x := 0; x < len(runes); x++ {
203 screen.SetContent(x, h-1, runes[x], nil, m.style)
207 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)
212 // A GutterMessage is a message displayed on the side of the editor
213 type GutterMessage struct {
219 // These are the different types of messages
221 // GutterInfo represents a simple info message
223 // GutterWarning represents a compiler warning
225 // GutterError represents a compiler error