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
61 // Message sends a message to the user
62 func (m *Messenger) Message(msg ...interface{}) {
63 buf := new(bytes.Buffer)
64 fmt.Fprint(buf, msg...)
65 m.message = buf.String()
68 if _, ok := colorscheme["message"]; ok {
69 m.style = colorscheme["message"]
74 // Error sends an error message to the user
75 func (m *Messenger) Error(msg ...interface{}) {
76 buf := new(bytes.Buffer)
77 fmt.Fprint(buf, msg...)
78 m.message = buf.String()
80 Foreground(tcell.ColorBlack).
81 Background(tcell.ColorMaroon)
83 if _, ok := colorscheme["error-message"]; ok {
84 m.style = colorscheme["error-message"]
89 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
90 func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
97 event := screen.PollEvent()
99 switch e := event.(type) {
100 case *tcell.EventKey:
105 } else if e.Rune() == 'n' {
108 case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
115 // Prompt sends the user a message and waits for a response to be typed in
116 // This function blocks the main loop while waiting for input
117 func (m *Messenger) Prompt(prompt string) (string, bool) {
121 response, canceled := "", true
127 event := screen.PollEvent()
129 switch e := event.(type) {
130 case *tcell.EventKey:
132 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
136 // User is done entering their response
138 response, canceled = m.response, false
151 return response, canceled
154 // HandleEvent handles an event for the prompter
155 func (m *Messenger) HandleEvent(event tcell.Event) {
156 switch e := event.(type) {
157 case *tcell.EventKey:
164 if m.cursorx < Count(m.response) {
167 case tcell.KeyBackspace2:
169 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)