]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/colorscheme.go
Code optimisation (#1117)
[micro.git] / cmd / micro / colorscheme.go
index 74e770181b181744a30a7d37b1b8c33df01cbb1f..8a649b1785d606d4149acfae214b86d5bbb0b63c 100644 (file)
@@ -15,6 +15,33 @@ type Colorscheme map[string]tcell.Style
 // The current colorscheme
 var colorscheme Colorscheme
 
+// GetColor takes in a syntax group and returns the colorscheme's style for that group
+func GetColor(color string) tcell.Style {
+       st := defStyle
+       if color == "" {
+               return st
+       }
+       groups := strings.Split(color, ".")
+       if len(groups) > 1 {
+               curGroup := ""
+               for i, g := range groups {
+                       if i != 0 {
+                               curGroup += "."
+                       }
+                       curGroup += g
+                       if style, ok := colorscheme[curGroup]; ok {
+                               st = style
+                       }
+               }
+       } else if style, ok := colorscheme[color]; ok {
+               st = style
+       } else {
+               st = StringToStyle(color)
+       }
+
+       return st
+}
+
 // ColorschemeExists checks if a given colorscheme exists
 func ColorschemeExists(colorschemeName string) bool {
        return FindRuntimeFile(RTColorscheme, colorschemeName) != nil
@@ -23,10 +50,11 @@ func ColorschemeExists(colorschemeName string) bool {
 // InitColorscheme picks and initializes the colorscheme when micro starts
 func InitColorscheme() {
        colorscheme = make(Colorscheme)
+       defStyle = tcell.StyleDefault.
+               Foreground(tcell.ColorDefault).
+               Background(tcell.ColorDefault)
        if screen != nil {
-               screen.SetStyle(tcell.StyleDefault.
-                       Foreground(tcell.ColorDefault).
-                       Background(tcell.ColorDefault))
+               // screen.SetStyle(defStyle)
        }
 
        LoadDefaultColorscheme()
@@ -47,20 +75,6 @@ func LoadColorscheme(colorschemeName string) {
                        TermMessage("Error loading colorscheme:", err)
                } else {
                        colorscheme = ParseColorscheme(string(data))
-
-                       // Default style
-                       defStyle = tcell.StyleDefault.
-                               Foreground(tcell.ColorDefault).
-                               Background(tcell.ColorDefault)
-
-                       // There may be another default style defined in the colorscheme
-                       // In that case we should use that one
-                       if style, ok := colorscheme["default"]; ok {
-                               defStyle = style
-                       }
-                       if screen != nil {
-                               screen.SetStyle(defStyle)
-                       }
                }
        }
 }
@@ -94,6 +108,9 @@ func ParseColorscheme(text string) Colorscheme {
                        if link == "default" {
                                defStyle = style
                        }
+                       if screen != nil {
+                               // screen.SetStyle(defStyle)
+                       }
                } else {
                        fmt.Println("Color-link statement is not valid:", line)
                }
@@ -107,7 +124,13 @@ func ParseColorscheme(text string) Colorscheme {
 // The 'extra' can be bold, reverse, or underline
 func StringToStyle(str string) tcell.Style {
        var fg, bg string
-       split := strings.Split(str, ",")
+       spaceSplit := strings.Split(str, " ")
+       var split []string
+       if len(spaceSplit) > 1 {
+               split = strings.Split(spaceSplit[1], ",")
+       } else {
+               split = strings.Split(str, ",")
+       }
        if len(split) > 1 {
                fg, bg = split[0], split[1]
        } else {
@@ -229,5 +252,9 @@ func GetColor256(color int) tcell.Color {
                tcell.Color253, tcell.Color254, tcell.Color255,
        }
 
-       return colors[color]
+       if color >= 0 && color < len(colors) {
+               return colors[color]
+       }
+
+       return tcell.ColorDefault
 }