10 "github.com/gdamore/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{}) {
21 fmt.Print("\nPress enter to continue")
23 reader := bufio.NewReader(os.Stdin)
24 reader.ReadString('\n')
27 // TermError sends an error to the user in the terminal. Like TermMessage except formatted
29 func TermError(filename string, lineNum int, err string) {
30 TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err)
33 // Messenger is an object that makes it easy to send messages to the user
34 // and get input from the user
35 type Messenger struct {
36 // Are we currently prompting the user?
38 // Is there a message to print
43 // The user's response to a prompt
45 // style to use when drawing the message
48 // We have to keep track of the cursor for prompting
52 // Message sends a message to the user
53 func (m *Messenger) Message(msg ...interface{}) {
54 buf := new(bytes.Buffer)
55 fmt.Fprint(buf, msg...)
56 m.message = buf.String()
59 if _, ok := colorscheme["message"]; ok {
60 m.style = colorscheme["message"]
65 // Error sends an error message to the user
66 func (m *Messenger) Error(msg ...interface{}) {
67 buf := new(bytes.Buffer)
68 fmt.Fprint(buf, msg...)
69 m.message = buf.String()
71 Foreground(tcell.ColorBlack).
72 Background(tcell.ColorMaroon)
74 if _, ok := colorscheme["error-message"]; ok {
75 m.style = colorscheme["error-message"]
80 // YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
81 func (m *Messenger) YesNoPrompt(prompt string) bool {
88 event := screen.PollEvent()
90 switch e := event.(type) {
92 if e.Key() == tcell.KeyRune {
95 } else if e.Rune() == 'n' {
103 // Prompt sends the user a message and waits for a response to be typed in
104 // This function blocks the main loop while waiting for input
105 func (m *Messenger) Prompt(prompt string) (string, bool) {
109 response, canceled := "", true
115 event := screen.PollEvent()
117 switch e := event.(type) {
118 case *tcell.EventKey:
120 case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
124 // User is done entering their response
126 response, canceled = m.response, false
139 return response, canceled
142 // HandleEvent handles an event for the prompter
143 func (m *Messenger) HandleEvent(event tcell.Event) {
144 switch e := event.(type) {
145 case *tcell.EventKey:
152 if m.cursorx < Count(m.response) {
155 case tcell.KeyBackspace2:
157 m.response = string([]rune(m.response)[:m.cursorx-1]) + string(m.response[m.cursorx:])
164 m.response = Insert(m.response, m.cursorx, string(e.Rune()))
170 // Reset resets the messenger's cursor, message and response
171 func (m *Messenger) Reset() {
177 // Clear clears the line at the bottom of the editor
178 func (m *Messenger) Clear() {
179 w, h := screen.Size()
180 for x := 0; x < w; x++ {
181 screen.SetContent(x, h-1, ' ', nil, defStyle)
185 // Display displays messages or prompts
186 func (m *Messenger) Display() {
187 _, h := screen.Size()
189 runes := []rune(m.message + m.response)
190 for x := 0; x < len(runes); x++ {
191 screen.SetContent(x, h-1, runes[x], nil, m.style)
195 screen.ShowCursor(Count(m.message)+m.cursorx, h-1)