X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fcolorscheme.go;h=f0c8adf2fd682beba699fe5f0688d18430f46b21;hb=924809b19baa0b7e6da896a35624934f96a26019;hp=e57fabeb329ae6a9e070cb1308f3d7a99696fe59;hpb=6489f4b6e811ec2f4702d4273a7e4879904291ef;p=micro.git diff --git a/cmd/micro/colorscheme.go b/cmd/micro/colorscheme.go index e57fabeb..f0c8adf2 100644 --- a/cmd/micro/colorscheme.go +++ b/cmd/micro/colorscheme.go @@ -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,40 +15,52 @@ type Colorscheme map[string]tcell.Style // The current colorscheme var colorscheme Colorscheme -var preInstalledColors = []string{"default", "solarized", "solarized-tc", "atom-dark-tc", "monokai"} +// 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) + if screen != nil { + screen.SetStyle(tcell.StyleDefault. + Foreground(tcell.ColorDefault). + Background(tcell.ColorDefault)) + } + LoadDefaultColorscheme() } // LoadDefaultColorscheme loads the default colorscheme from $(configDir)/colorschemes func LoadDefaultColorscheme() { - LoadColorscheme(settings["colorscheme"].(string), 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)) - } - } +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)) - 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 + // 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) } - colorscheme = ParseColorscheme(string(data)) } } } @@ -76,7 +88,12 @@ 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 + } } else { fmt.Println("Color-link statement is not valid:", line) } @@ -89,9 +106,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 +122,19 @@ func StringToStyle(str string) tcell.Style { fg = strings.TrimSpace(fg) bg = strings.TrimSpace(bg) - style := defStyle.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) }