]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/messenger.go
Fix: mouse clicking with softwrap
[micro.git] / cmd / micro / messenger.go
index 7e641735b1eca3cbfbb76f9802bbf520ed1914a1..5cfdf1c2c85e6071f12cd39593a63ea628b9cc72 100644 (file)
@@ -6,6 +6,7 @@ import (
        "fmt"
        "os"
        "strconv"
+       "strings"
 
        "github.com/zyedidia/clipboard"
        "github.com/zyedidia/tcell"
@@ -21,6 +22,7 @@ func TermMessage(msg ...interface{}) {
        screenWasNil := screen == nil
        if !screenWasNil {
                screen.Fini()
+               screen = nil
        }
 
        fmt.Println(msg...)
@@ -43,6 +45,7 @@ func TermError(filename string, lineNum int, err string) {
 // Messenger is an object that makes it easy to send messages to the user
 // and get input from the user
 type Messenger struct {
+       log *Buffer
        // Are we currently prompting the user?
        hasPrompt bool
        // Is there a message to print
@@ -67,16 +70,30 @@ type Messenger struct {
        gutterMessage bool
 }
 
+func (m *Messenger) AddLog(msg string) {
+       buffer := m.getBuffer()
+       buffer.insert(buffer.End(), []byte(msg+"\n"))
+       buffer.Cursor.Loc = buffer.End()
+       buffer.Cursor.Relocate()
+}
+
+func (m *Messenger) getBuffer() *Buffer {
+       if m.log == nil {
+               m.log = NewBuffer(strings.NewReader(""), "")
+               m.log.name = "Log"
+       }
+       return m.log
+}
+
 // Message sends a message to the user
 func (m *Messenger) Message(msg ...interface{}) {
-       buf := new(bytes.Buffer)
-       fmt.Fprint(buf, msg...)
-       m.message = buf.String()
+       m.message = fmt.Sprint(msg...)
        m.style = defStyle
 
        if _, ok := colorscheme["message"]; ok {
                m.style = colorscheme["message"]
        }
+       m.AddLog(m.message)
        m.hasMessage = true
 }
 
@@ -92,6 +109,7 @@ func (m *Messenger) Error(msg ...interface{}) {
        if _, ok := colorscheme["error-message"]; ok {
                m.style = colorscheme["error-message"]
        }
+       m.AddLog(m.message)
        m.hasMessage = true
 }
 
@@ -113,13 +131,16 @@ func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
                        switch e.Key() {
                        case tcell.KeyRune:
                                if e.Rune() == 'y' {
+                                       m.AddLog("\t--> y")
                                        m.hasPrompt = false
                                        return true, false
                                } else if e.Rune() == 'n' {
+                                       m.AddLog("\t--> n")
                                        m.hasPrompt = false
                                        return false, false
                                }
                        case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
+                               m.AddLog("\t--> (cancel)")
                                m.hasPrompt = false
                                return false, true
                        }
@@ -146,6 +167,7 @@ func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool)
                        case tcell.KeyRune:
                                for _, r := range responses {
                                        if e.Rune() == r {
+                                               m.AddLog("\t--> " + string(r))
                                                m.Clear()
                                                m.Reset()
                                                m.hasPrompt = false
@@ -153,6 +175,7 @@ func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool)
                                        }
                                }
                        case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
+                               m.AddLog("\t--> (cancel)")
                                m.Clear()
                                m.Reset()
                                m.hasPrompt = false
@@ -170,11 +193,13 @@ const (
        CommandCompletion
        HelpCompletion
        OptionCompletion
+       PluginCmdCompletion
+       PluginNameCompletion
 )
 
 // Prompt sends the user a message and waits for a response to be typed in
 // This function blocks the main loop while waiting for input
-func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Completion) (string, bool) {
+func (m *Messenger) Prompt(prompt, placeholder, historyType string, completionTypes ...Completion) (string, bool) {
        m.hasPrompt = true
        m.Message(prompt)
        if _, ok := m.history[historyType]; !ok {
@@ -184,7 +209,9 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
        }
        m.historyNum = len(m.history[historyType]) - 1
 
-       response, canceled := "", true
+       response, canceled := placeholder, true
+       m.response = response
+       m.cursorx = Count(placeholder)
 
        RedrawAll()
        for m.hasPrompt {
@@ -198,9 +225,11 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
                        switch e.Key() {
                        case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
                                // Cancel
+                               m.AddLog("\t--> (cancel)")
                                m.hasPrompt = false
                        case tcell.KeyEnter:
                                // User is done entering their response
+                               m.AddLog("\t--> " + m.response)
                                m.hasPrompt = false
                                response, canceled = m.response, false
                                m.history[historyType][len(m.history[historyType])-1] = response
@@ -231,6 +260,10 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
                                        chosen, suggestions = HelpComplete(currentArg)
                                } else if completionType == OptionCompletion {
                                        chosen, suggestions = OptionComplete(currentArg)
+                               } else if completionType == PluginCmdCompletion {
+                                       chosen, suggestions = PluginCmdComplete(currentArg)
+                               } else if completionType == PluginNameCompletion {
+                                       chosen, suggestions = PluginNameComplete(currentArg)
                                } else if completionType < NoCompletion {
                                        chosen, suggestions = PluginComplete(completionType, currentArg)
                                }