]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/colorscheme.go
Code optimisation (#1117)
[micro.git] / cmd / micro / colorscheme.go
index 597bc541fb95bed32a78e7864974632e3e892ea7..8a649b1785d606d4149acfae214b86d5bbb0b63c 100644 (file)
@@ -2,11 +2,11 @@ package main
 
 import (
        "fmt"
-       "github.com/zyedidia/tcell"
-       "io/ioutil"
        "regexp"
        "strconv"
        "strings"
+
+       "github.com/zyedidia/tcell"
 )
 
 // Colorscheme is a map from string to style -- it represents a colorscheme
@@ -15,39 +15,65 @@ type Colorscheme map[string]tcell.Style
 // The current colorscheme
 var colorscheme Colorscheme
 
-var preInstalledColors = [3]string{"default", "solarized", "solarized-tc"}
+// 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
+}
 
 // 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(defStyle)
+       }
+
        LoadDefaultColorscheme()
 }
 
 // LoadDefaultColorscheme loads the default colorscheme from $(configDir)/colorschemes
 func LoadDefaultColorscheme() {
-       LoadColorscheme(settings.Colorscheme, configDir+"/colorschemes")
+       LoadColorscheme(globalSettings["colorscheme"].(string))
 }
 
 // LoadColorscheme loads the given colorscheme from a directory
-func LoadColorscheme(colorschemeName, dir string) {
-       files, _ := ioutil.ReadDir(dir)
-       for _, f := range files {
-               if f.Name() == colorschemeName+".micro" {
-                       text, err := ioutil.ReadFile(dir + "/" + f.Name())
-                       if err != nil {
-                               fmt.Println("Error loading colorscheme:", err)
-                               continue
-                       }
-                       colorscheme = ParseColorscheme(string(text))
-               }
-       }
-
-       for _, name := range preInstalledColors {
-               if name == colorschemeName {
-                       data, err := Asset("runtime/colorschemes/" + name + ".micro")
-                       if err != nil {
-                               TermMessage("Unable to load pre-installed colorscheme " + name)
-                               continue
-                       }
+func LoadColorscheme(colorschemeName string) {
+       file := FindRuntimeFile(RTColorscheme, colorschemeName)
+       if file == nil {
+               TermMessage(colorschemeName, "is not a valid colorscheme")
+       } else {
+               if data, err := file.Data(); err != nil {
+                       TermMessage("Error loading colorscheme:", err)
+               } else {
                        colorscheme = ParseColorscheme(string(data))
                }
        }
@@ -76,7 +102,15 @@ func ParseColorscheme(text string) Colorscheme {
                        link := string(matches[1])
                        colors := string(matches[2])
 
-                       c[link] = StringToStyle(colors)
+                       style := StringToStyle(colors)
+                       c[link] = style
+
+                       if link == "default" {
+                               defStyle = style
+                       }
+                       if screen != nil {
+                               // screen.SetStyle(defStyle)
+                       }
                } else {
                        fmt.Println("Color-link statement is not valid:", line)
                }
@@ -89,9 +123,14 @@ func ParseColorscheme(text string) Colorscheme {
 // The strings must be in the format "extra foregroundcolor,backgroundcolor"
 // The 'extra' can be bold, reverse, or underline
 func StringToStyle(str string) tcell.Style {
-       var fg string
-       bg := "default"
-       split := strings.Split(str, ",")
+       var fg, bg string
+       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 {
@@ -100,7 +139,19 @@ func StringToStyle(str string) tcell.Style {
        fg = strings.TrimSpace(fg)
        bg = strings.TrimSpace(bg)
 
-       style := tcell.StyleDefault.Foreground(StringToColor(fg)).Background(StringToColor(bg))
+       var fgColor, bgColor tcell.Color
+       if fg == "" {
+               fgColor, _, _ = defStyle.Decompose()
+       } else {
+               fgColor = StringToColor(fg)
+       }
+       if bg == "" {
+               _, bgColor, _ = defStyle.Decompose()
+       } else {
+               bgColor = StringToColor(bg)
+       }
+
+       style := defStyle.Foreground(fgColor).Background(bgColor)
        if strings.Contains(str, "bold") {
                style = style.Bold(true)
        }
@@ -201,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
 }