6 "github.com/gdamore/tcell"
11 // TermMessage sends a message to the user in the terminal. This usually occurs before
12 // micro has been fully initialized -- ie if there is an error in the syntax highlighting
13 // regular expressions
14 // The function must be called when the screen is not initialized
15 // This will write the message, and wait for the user
16 // to press and key to continue
17 func TermMessage(msg string) {
19 fmt.Print("\nPress enter to continue")
21 reader := bufio.NewReader(os.Stdin)
22 reader.ReadString('\n')
25 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
27 func TermError(filename string, lineNum int, err string) {
28 TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
31 // Messenger is an object that makes it easy to send messages to the user
32 // and get input from the user
33 type Messenger struct {
34 // Are we currently prompting the user?
36 // Is there a message to print
41 // The user's response to a prompt
43 // style to use when drawing the message
46 // We have to keep track of the cursor for prompting
50 // Message sends a message to the user
51 func (m *Messenger) Message(msg string) {
55 if _, ok := colorscheme["message"]; ok {
56 m.style = colorscheme["message"]
61 // Error sends an error message to the user
62 func (m *Messenger) Error(msg string) {
65 Foreground(tcell.ColorBlack).
66 Background(tcell.ColorMaroon)
68 if _, ok := colorscheme["error-message"]; ok {
69 m.style = colorscheme["error-message"]
74 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
75 func (m *Messenger) YesNoPrompt(prompt string) bool {
82 event := screen.PollEvent()
84 switch e := event.(type) {
86 if e.Key() == tcell.KeyRune {
89 } else if e.Rune() == 'n' {
97 // Prompt sends the user a message and waits for a response to be typed in
98 // This function blocks the main loop while waiting for input
99 func (m *Messenger) Prompt(prompt string) (string, bool) {
103 response, canceled := "", true
109 event := screen.PollEvent()
111 switch e := event.(type) {
112 case *tcell.EventKey:
114 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
118 // User is done entering their response
120 response, canceled = m.response, false
133 return response, canceled
136 // HandleEvent handles an event for the prompter
137 func (m *Messenger) HandleEvent(event tcell.Event) {
138 switch e := event.(type) {
139 case *tcell.EventKey:
146 if m.cursorx < Count(m.response) {
149 case tcell.KeyBackspace2:
151 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
158 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
164 // Reset resets the messenger's cursor, message and response
165 func (m *Messenger) Reset() {
171 // Clear clears the line at the bottom of the editor
172 func (m *Messenger) Clear() {
173 w, h := screen.Size()
174 for x := 0; x < w; x++ {
175 screen.SetContent(x, h-1, ' ', nil, defStyle)
179 // Display displays messages or prompts
180 func (m *Messenger) Display() {
181 _, h := screen.Size()
183 runes := []rune(m.message + m.response)
184 for x := 0; x < len(runes); x++ {
185 screen.SetContent(x, h-1, runes[x], nil, m.style)
189 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)