]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Use a map for settings instead of a struct
[micro.git] / cmd / micro / view.go
index 6ac769ad2c6e1db7ae7d6be928a1a75c67a5e9f2..25323bd524b829215a5c2d9595807dd30eaeb7ab 100644 (file)
@@ -6,7 +6,7 @@ import (
        "strings"
        "time"
 
-       "github.com/gdamore/tcell"
+       "github.com/zyedidia/tcell"
 )
 
 // The View struct stores information about a view into a buffer.
@@ -34,6 +34,8 @@ type View struct {
        // The eventhandler for undo/redo
        eh *EventHandler
 
+       messages []GutterMessage
+
        // The buffer
        buf *Buffer
        // The statusline
@@ -137,7 +139,7 @@ func (v *View) CanClose(msg string) bool {
                        if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
                                return true
                        } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
-                               Save(v)
+                               v.Save()
                                return true
                        }
                }
@@ -277,9 +279,9 @@ func (v *View) HandleEvent(event tcell.Event) {
                case tcell.Button1:
                        // Left click
                        origX, origY := v.cursor.x, v.cursor.y
-                       v.MoveToMouseClick(x, y)
 
-                       if v.mouseReleased {
+                       if v.mouseReleased && !e.HasMotion() {
+                               v.MoveToMouseClick(x, y)
                                if (time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold) &&
                                        (origX == v.cursor.x && origY == v.cursor.y) {
                                        if v.doubleClick {
@@ -308,7 +310,9 @@ func (v *View) HandleEvent(event tcell.Event) {
                                        v.cursor.curSelection[0] = loc
                                        v.cursor.curSelection[1] = loc
                                }
-                       } else {
+                               v.mouseReleased = false
+                       } else if !v.mouseReleased {
+                               v.MoveToMouseClick(x, y)
                                if v.tripleClick {
                                        v.cursor.AddLineToSelection()
                                } else if v.doubleClick {
@@ -317,7 +321,6 @@ func (v *View) HandleEvent(event tcell.Event) {
                                        v.cursor.curSelection[1] = v.cursor.Loc()
                                }
                        }
-                       v.mouseReleased = false
                case tcell.ButtonNone:
                        // Mouse event with no click
                        if !v.mouseReleased {
@@ -354,11 +357,26 @@ func (v *View) HandleEvent(event tcell.Event) {
        if relocate {
                v.Relocate()
        }
-       if settings.Syntax {
+       if settings["syntax"].(bool) {
                v.matches = Match(v)
        }
 }
 
+// GutterMessage creates a message in this view's gutter
+func (v *View) GutterMessage(lineN int, msg string, kind int) {
+       gutterMsg := GutterMessage{
+               lineNum: lineN,
+               msg:     msg,
+               kind:    kind,
+       }
+       for _, gmsg := range v.messages {
+               if gmsg.lineNum == lineN {
+                       return
+               }
+       }
+       v.messages = append(v.messages, gutterMsg)
+}
+
 // DisplayView renders the view to the screen
 func (v *View) DisplayView() {
        // The character number of the character in the top left of the screen
@@ -368,13 +386,17 @@ func (v *View) DisplayView() {
        // We are going to have to offset by that amount
        maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
        // + 1 for the little space after the line number
-       if settings.Ruler == true {
+       if settings["ruler"] == true {
                v.lineNumOffset = maxLineLength + 1
        } else {
                v.lineNumOffset = 0
        }
        var highlightStyle tcell.Style
 
+       if len(v.messages) > 0 {
+               v.lineNumOffset += 2
+       }
+
        for lineN := 0; lineN < v.height; lineN++ {
                var x int
                // If the buffer is smaller than the view height
@@ -384,6 +406,48 @@ func (v *View) DisplayView() {
                }
                line := v.buf.lines[lineN+v.topline]
 
+               if len(v.messages) > 0 {
+                       msgOnLine := false
+                       for _, msg := range v.messages {
+                               if msg.lineNum == lineN+v.topline {
+                                       msgOnLine = true
+                                       gutterStyle := tcell.StyleDefault
+                                       switch msg.kind {
+                                       case GutterInfo:
+                                               if style, ok := colorscheme["gutter-info"]; ok {
+                                                       gutterStyle = style
+                                               }
+                                       case GutterWarning:
+                                               if style, ok := colorscheme["gutter-warning"]; ok {
+                                                       gutterStyle = style
+                                               }
+                                       case GutterError:
+                                               if style, ok := colorscheme["gutter-error"]; ok {
+                                                       gutterStyle = style
+                                               }
+                                       }
+                                       screen.SetContent(x, lineN, '>', nil, gutterStyle)
+                                       x++
+                                       screen.SetContent(x, lineN, '>', nil, gutterStyle)
+                                       x++
+                                       if v.cursor.y == lineN {
+                                               messenger.Message(msg.msg)
+                                               messenger.gutterMessage = true
+                                       }
+                               }
+                       }
+                       if !msgOnLine {
+                               screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
+                               x++
+                               screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
+                               x++
+                               if v.cursor.y == lineN && messenger.gutterMessage {
+                                       messenger.Reset()
+                                       messenger.gutterMessage = false
+                               }
+                       }
+               }
+
                // Write the line number
                lineNumStyle := defStyle
                if style, ok := colorscheme["line-number"]; ok {
@@ -391,7 +455,7 @@ func (v *View) DisplayView() {
                }
                // Write the spaces before the line number if necessary
                var lineNum string
-               if settings.Ruler == true {
+               if settings["ruler"] == true {
                        lineNum = strconv.Itoa(lineN + v.topline + 1)
                        for i := 0; i < maxLineLength-len(lineNum); i++ {
                                screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
@@ -403,7 +467,7 @@ func (v *View) DisplayView() {
                                x++
                        }
 
-                       if settings.Ruler == true {
+                       if settings["ruler"] == true {
                                // Write the extra space
                                screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
                                x++
@@ -414,7 +478,7 @@ func (v *View) DisplayView() {
                for colN, ch := range line {
                        var lineStyle tcell.Style
 
-                       if settings.Syntax {
+                       if settings["syntax"].(bool) {
                                // Syntax highlighting is enabled
                                highlightStyle = v.matches[lineN][colN]
                        }
@@ -434,7 +498,7 @@ func (v *View) DisplayView() {
 
                        if ch == '\t' {
                                screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
-                               tabSize := settings.TabSize
+                               tabSize := int(settings["tabsize"].(float64))
                                for i := 0; i < tabSize-1; i++ {
                                        tabchars++
                                        if x-v.leftCol+tabchars >= v.lineNumOffset {