| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"regexp" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/micro-editor/tcell/v2" |
| 10 |
) |
| 11 |
|
| 12 |
// DefStyle is Micro's default style |
| 13 |
var DefStyle tcell.Style = tcell.StyleDefault |
| 14 |
|
| 15 |
// Colorscheme is the current colorscheme |
| 16 |
var Colorscheme map[string]tcell.Style |
| 17 |
|
| 18 |
// GetColor takes in a syntax group and returns the colorscheme's style for that group |
| 19 |
func GetColor(color string) tcell.Style { |
| 20 |
st := DefStyle |
| 21 |
if color == "" { |
| 22 |
return st |
| 23 |
} |
| 24 |
groups := strings.Split(color, ".") |
| 25 |
if len(groups) > 1 { |
| 26 |
curGroup := "" |
| 27 |
for i, g := range groups { |
| 28 |
if i != 0 { |
| 29 |
curGroup += "." |
| 30 |
} |
| 31 |
curGroup += g |
| 32 |
if style, ok := Colorscheme[curGroup]; ok { |
| 33 |
st = style |
| 34 |
} |
| 35 |
} |
| 36 |
} else if style, ok := Colorscheme[color]; ok { |
| 37 |
st = style |
| 38 |
} else { |
| 39 |
st = StringToStyle(color) |
| 40 |
} |
| 41 |
|
| 42 |
return st |
| 43 |
} |
| 44 |
|
| 45 |
// ColorschemeExists checks if a given colorscheme exists |
| 46 |
func ColorschemeExists(colorschemeName string) bool { |
| 47 |
return FindRuntimeFile(RTColorscheme, colorschemeName) != nil |
| 48 |
} |
| 49 |
|
| 50 |
// InitColorscheme picks and initializes the colorscheme when micro starts |
| 51 |
func InitColorscheme() error { |
| 52 |
Colorscheme = make(map[string]tcell.Style) |
| 53 |
DefStyle = tcell.StyleDefault |
| 54 |
|
| 55 |
c, err := LoadDefaultColorscheme() |
| 56 |
if err == nil { |
| 57 |
Colorscheme = c |
| 58 |
} else { |
| 59 |
// The colorscheme setting seems broken (maybe because we have not validated |
| 60 |
// it earlier, see comment in verifySetting()). So reset it to the default |
| 61 |
// colorscheme and try again. |
| 62 |
GlobalSettings["colorscheme"] = DefaultGlobalOnlySettings["colorscheme"] |
| 63 |
if c, err2 := LoadDefaultColorscheme(); err2 == nil { |
| 64 |
Colorscheme = c |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return err |
| 69 |
} |
| 70 |
|
| 71 |
// LoadDefaultColorscheme loads the default colorscheme from $(ConfigDir)/colorschemes |
| 72 |
func LoadDefaultColorscheme() (map[string]tcell.Style, error) { |
| 73 |
var parsedColorschemes []string |
| 74 |
return LoadColorscheme(GlobalSettings["colorscheme"].(string), &parsedColorschemes) |
| 75 |
} |
| 76 |
|
| 77 |
// LoadColorscheme loads the given colorscheme from a directory |
| 78 |
func LoadColorscheme(colorschemeName string, parsedColorschemes *[]string) (map[string]tcell.Style, error) { |
| 79 |
c := make(map[string]tcell.Style) |
| 80 |
file := FindRuntimeFile(RTColorscheme, colorschemeName) |
| 81 |
if file == nil { |
| 82 |
return c, errors.New(colorschemeName + " is not a valid colorscheme") |
| 83 |
} |
| 84 |
if data, err := file.Data(); err != nil { |
| 85 |
return c, errors.New("Error loading colorscheme: " + err.Error()) |
| 86 |
} else { |
| 87 |
var err error |
| 88 |
c, err = ParseColorscheme(file.Name(), string(data), parsedColorschemes) |
| 89 |
if err != nil { |
| 90 |
return c, err |
| 91 |
} |
| 92 |
} |
| 93 |
return c, nil |
| 94 |
} |
| 95 |
|
| 96 |
// ParseColorscheme parses the text definition for a colorscheme and returns the corresponding object |
| 97 |
// Colorschemes are made up of color-link statements linking a color group to a list of colors |
| 98 |
// For example, color-link keyword (blue,red) makes all keywords have a blue foreground and |
| 99 |
// red background |
| 100 |
func ParseColorscheme(name string, text string, parsedColorschemes *[]string) (map[string]tcell.Style, error) { |
| 101 |
var err error |
| 102 |
colorParser := regexp.MustCompile(`color-link\s+(\S*)\s+"(.*)"`) |
| 103 |
includeParser := regexp.MustCompile(`include\s+"(.*)"`) |
| 104 |
lines := strings.Split(text, "\n") |
| 105 |
c := make(map[string]tcell.Style) |
| 106 |
|
| 107 |
if parsedColorschemes != nil { |
| 108 |
*parsedColorschemes = append(*parsedColorschemes, name) |
| 109 |
} |
| 110 |
|
| 111 |
lineLoop: |
| 112 |
for _, line := range lines { |
| 113 |
if strings.TrimSpace(line) == "" || |
| 114 |
strings.TrimSpace(line)[0] == '#' { |
| 115 |
// Ignore this line |
| 116 |
continue |
| 117 |
} |
| 118 |
|
| 119 |
matches := includeParser.FindSubmatch([]byte(line)) |
| 120 |
if len(matches) == 2 { |
| 121 |
// support includes only in case parsedColorschemes are given |
| 122 |
if parsedColorschemes != nil { |
| 123 |
include := string(matches[1]) |
| 124 |
for _, name := range *parsedColorschemes { |
| 125 |
// check for circular includes... |
| 126 |
if name == include { |
| 127 |
// ...and prevent them |
| 128 |
continue lineLoop |
| 129 |
} |
| 130 |
} |
| 131 |
includeScheme, err := LoadColorscheme(include, parsedColorschemes) |
| 132 |
if err != nil { |
| 133 |
return c, err |
| 134 |
} |
| 135 |
for k, v := range includeScheme { |
| 136 |
c[k] = v |
| 137 |
} |
| 138 |
} |
| 139 |
continue |
| 140 |
} |
| 141 |
|
| 142 |
matches = colorParser.FindSubmatch([]byte(line)) |
| 143 |
if len(matches) == 3 { |
| 144 |
link := string(matches[1]) |
| 145 |
colors := string(matches[2]) |
| 146 |
|
| 147 |
style := StringToStyle(colors) |
| 148 |
c[link] = style |
| 149 |
|
| 150 |
if link == "default" { |
| 151 |
DefStyle = style |
| 152 |
} |
| 153 |
} else { |
| 154 |
err = errors.New("Color-link statement is not valid: " + line) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return c, err |
| 159 |
} |
| 160 |
|
| 161 |
// StringToStyle returns a style from a string |
| 162 |
// The strings must be in the format "extra foregroundcolor,backgroundcolor" |
| 163 |
// The 'extra' can be bold, reverse, italic or underline |
| 164 |
func StringToStyle(str string) tcell.Style { |
| 165 |
var fg, bg string |
| 166 |
spaceSplit := strings.Split(str, " ") |
| 167 |
split := strings.Split(spaceSplit[len(spaceSplit)-1], ",") |
| 168 |
if len(split) > 1 { |
| 169 |
fg, bg = split[0], split[1] |
| 170 |
} else { |
| 171 |
fg = split[0] |
| 172 |
} |
| 173 |
fg = strings.TrimSpace(fg) |
| 174 |
bg = strings.TrimSpace(bg) |
| 175 |
|
| 176 |
var fgColor, bgColor tcell.Color |
| 177 |
var ok bool |
| 178 |
if fg == "" || fg == "default" { |
| 179 |
fgColor, _, _ = DefStyle.Decompose() |
| 180 |
} else { |
| 181 |
fgColor, ok = StringToColor(fg) |
| 182 |
if !ok { |
| 183 |
fgColor, _, _ = DefStyle.Decompose() |
| 184 |
} |
| 185 |
} |
| 186 |
if bg == "" || bg == "default" { |
| 187 |
_, bgColor, _ = DefStyle.Decompose() |
| 188 |
} else { |
| 189 |
bgColor, ok = StringToColor(bg) |
| 190 |
if !ok { |
| 191 |
_, bgColor, _ = DefStyle.Decompose() |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
style := DefStyle.Foreground(fgColor).Background(bgColor) |
| 196 |
if strings.Contains(str, "bold") { |
| 197 |
style = style.Bold(true) |
| 198 |
} |
| 199 |
if strings.Contains(str, "italic") { |
| 200 |
style = style.Italic(true) |
| 201 |
} |
| 202 |
if strings.Contains(str, "reverse") { |
| 203 |
style = style.Reverse(true) |
| 204 |
} |
| 205 |
if strings.Contains(str, "underline") { |
| 206 |
style = style.Underline(true) |
| 207 |
} |
| 208 |
return style |
| 209 |
} |
| 210 |
|
| 211 |
// StringToColor returns a tcell color from a string representation of a color |
| 212 |
// We accept either bright... or light... to mean the brighter version of a color |
| 213 |
func StringToColor(str string) (tcell.Color, bool) { |
| 214 |
switch str { |
| 215 |
case "black": |
| 216 |
return tcell.ColorBlack, true |
| 217 |
case "red": |
| 218 |
return tcell.ColorMaroon, true |
| 219 |
case "green": |
| 220 |
return tcell.ColorGreen, true |
| 221 |
case "yellow": |
| 222 |
return tcell.ColorOlive, true |
| 223 |
case "blue": |
| 224 |
return tcell.ColorNavy, true |
| 225 |
case "magenta": |
| 226 |
return tcell.ColorPurple, true |
| 227 |
case "cyan": |
| 228 |
return tcell.ColorTeal, true |
| 229 |
case "white": |
| 230 |
return tcell.ColorSilver, true |
| 231 |
case "brightblack", "lightblack": |
| 232 |
return tcell.ColorGray, true |
| 233 |
case "brightred", "lightred": |
| 234 |
return tcell.ColorRed, true |
| 235 |
case "brightgreen", "lightgreen": |
| 236 |
return tcell.ColorLime, true |
| 237 |
case "brightyellow", "lightyellow": |
| 238 |
return tcell.ColorYellow, true |
| 239 |
case "brightblue", "lightblue": |
| 240 |
return tcell.ColorBlue, true |
| 241 |
case "brightmagenta", "lightmagenta": |
| 242 |
return tcell.ColorFuchsia, true |
| 243 |
case "brightcyan", "lightcyan": |
| 244 |
return tcell.ColorAqua, true |
| 245 |
case "brightwhite", "lightwhite": |
| 246 |
return tcell.ColorWhite, true |
| 247 |
case "default": |
| 248 |
return tcell.ColorDefault, true |
| 249 |
default: |
| 250 |
// Check if this is a 256 color |
| 251 |
if num, err := strconv.Atoi(str); err == nil { |
| 252 |
return GetColor256(num), true |
| 253 |
} |
| 254 |
// Check if this is a truecolor hex value |
| 255 |
if len(str) == 7 && str[0] == '#' { |
| 256 |
return tcell.GetColor(str), true |
| 257 |
} |
| 258 |
return tcell.ColorDefault, false |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// GetColor256 returns the tcell color for a number between 0 and 255 |
| 263 |
func GetColor256(color int) tcell.Color { |
| 264 |
if color == 0 { |
| 265 |
return tcell.ColorDefault |
| 266 |
} |
| 267 |
return tcell.PaletteColor(color) |
| 268 |
} |